You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
publicclassEventHandler:IList<Delegate>
or
publicclassEventHandler:List<Delegate>
Usage Examples
timer.Elapsed.Clear();
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 to EventHandler. 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).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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
From the discussion dotnet/csharplang#4536.
Beta Was this translation helpful? Give feedback.
All reactions