Dude said:
The link you sent is very relevant. It explains very well what is going
on and what are the options.
For thread safety, most examples I have seen use a Mutex or the lock()
operation to avoid this kind of problem.
You really don't want to hold a lock while calling a delegate, however.
I think the actual operator =()
of delegates might not be thread safe (could be worth to check).
Delegates are immutable, so it's thread safe. On the other hand,
there's no guarantee that you'll get the latest value unless you have a
memory barrier.
Concerning the Raise, of course there is no guarantee it will be
overriden. The idea would be for me to be able to override it in
specific class of a UI framework so I know I will be able to call the
raise method on it.
Right - so instead of doing that, just build a dictionary.
There are workarounds, its just a nice to have that would have made the
code cleaner. It would be effortless for Microsoft to allow overriding
the raise modifier.
Yes - I believe it was omitted for purposes of encapsulation. It makes
the event truly pub/sub.
The only good reason I see for not supporting this is because of
technical reasons (which I have a hard time figuring out since it works
all good for the other .NET languages)
I doubt that it was a technical reason. as you say, it's easy.
Not implementing it for the sake of good design or what other reason you
may have does not look relevant to me. It's not Microsoft job to decide
if I should have a nice design or not (who would they be to decide that
raising an event externally means bad design in the first place), their
aim should be to make coding as easy, fast and elegant as possible.
Supporting the Raise Modifier would definitely go that way.
I disagree. It's entirely Microsoft's job to encourage good design.
There are numerous areas in which C# *doesn't* let you shoot yourself
in the foot.
As it is you're already using reflection - with my suggestion you'd
move *away* from reflection to a neater design in the first place.
To me it's like if Microsoft would decide to generate compile errors
when finding public fields in a class. Public fields are just horrible
code, but it's not their job to establish design rules for you when it
comes to compile your code.
Personally I'd be fairly happy to prohibit public fields, but there we
go.
Not supporting Raise Modifier because it is a bad coding approach is
just as unrelevant as this.
Well, from personal experience I can't say I've *ever* wanted it, and
in your particular case there's a cleaner solution anyway. With your
method, you'd have to code up one Raise for every event. Using my
suggestion you'd use an EventList and have a single Raise method taking
a key. That key can be exposed in a stronger way than just the name -
e.g. as an enum. That allows simple parsing from straight strings, as
well as some compile-time safety if you want to raise the event
directly from code. Wins all round.