Fix the delegate type system #4536
Replies: 3 comments 9 replies
-
I don't believe this is something related to the language. You might want to take this to dotnet/runtime. Would you like me to move it there? |
Beta Was this translation helpful? Give feedback.
-
In fact, I did have some fragments of how to update the delegate type system, but didn't arrange them. In brief, my opinion is to drop multicast completely, and force multicast usage to use |
Beta Was this translation helpful? Give feedback.
-
As mentioned in #4535, delegates are immutable. When you add or remove a delegate from the event you're actually replacing the event's backing field with a new delegate instance. There is already a means in the language to "clear" that delegate field so that it points to no subscribers: set it to protected void ClearMyEvent() {
this.MyEvent = null;
} See here: Example |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Background and Motivation
I want to clean up all the delegates that are previously assigned to a timer's
Elapsed
event. I don't want it to fire the old delegates that have had the old parameters. I want the timer to fire the newest delegate with the newest parameters.From this small perspective, we can find out that there is a pitfall in the delegate type system. It might be a C# language design flaw. Based on the contributor of dotnet/runtime, it is about the ability to modify the contract of delegate and event. I hope the contributors of both repositories can have a discussion of this issue since I, as a common user, have not dived into the .NET platform.
Proposed API
Please make EventHandler as a IList that could be Clear(), Add(), ...
Just make something like:
or
Usage Examples
Alternative Designs
There are a lot of complaints about this issue. One of the examples:
It is not easy to remember/keep track of the old delegates that have been assigned to an event handler. Make
Clear()
method could make life tons' better.As contributor stated here, it could be solved by adding
clear
accessor toEventHandler
. But the contributor here believes the ability to modify the contract of delegate and event is the key to fix similar issues once and for all. I believe a more inclusive solution can address more problems in the future.Risks
Might take some time to implement and test, or even refactor all the runtime library. (I am not quite sure about that since I am just a usual user).
Relative issues
Beta Was this translation helpful? Give feedback.
All reactions