Delegates/events weirdness #5928
Replies: 3 comments 60 replies
-
Can you give a sample of code that demonstrates the problem you mention here? It's not readily apparent to me what you mean. |
Beta Was this translation helpful? Give feedback.
-
Restricting what the C# compiler allows can be achieved today by writing a Roslyn analyzer. Implicitly looping over the delegate invocation list and calling Task.WhenAll with the results, or even serially awaiting, could change the meaning of existing code. https://github.com/Techsola/AmbientTasks is a completely different take on this which says that event handlers should not be async, but they can start async things that are most properly tracked at a higher level than at the event invocation sites (which should often be agnostic as to whether something async is being triggered and especially as to whether the handler fails). |
Beta Was this translation helpful? Give feedback.
-
Note: i think if you really want this sort of behavior, i would not invoke delegates manually. I would use GetInvocationList and control exactly how you think every individual delegate should be treated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I always found it strange how .NET chose to implement delegates, specifically WRT "multi-casting". This is not a language request, more a general discussion - I understand this cannot be changed, short of a full framework re-write. (Or maybe a new delegate system can be created alongside the existing one?)
Basically, right now, any delegate is a multi-cast delegate with some sort of linked list structure behind the scenes. This has already caused me at least one bug where I forgot that I have to
await
everyTask
in the invocation list, not just the first one. I'm sure people also had bugs where other types of return values were lost.I think it should be simplified to a single invocation delegate, and the
event
keyword auto-accessorsadd
andremove
should create a linked list typed backing field. Also,event
s should restrict the return type to eithervoid
orTask
(non-value returning task-like type); that way, it can be ensured that everything is raised correctly and no return values are lost. Below is how I envision it working:PS. For now, I think the only reasonable thing to do is create my own type, which is exposed as a get-only member (readonly field or get-property), and that type does the async invocations correctly and enforces type safety and whatnot.
Beta Was this translation helpful? Give feedback.
All reactions