MulticastDelegate and Exception handling: is it possible to wrap it all generically?

后端 未结 3 2104
逝去的感伤
逝去的感伤 2021-01-06 16:13

When calling a Multicast Delegate one should use GetInvocationList to call one by one the delegate:

public void IterateAll()
{
    if( _doExecute != null )
          


        
3条回答
  •  心在旅途
    2021-01-06 16:55

    Hmya, this is very fishy. Catching an exception and handling it only really works well when you can restore the program's state so that it looks like the exception never happened. That is completely impossible to do for a MulticastDelegate.

    It's contract is that you have no idea what kind of code subscribed an event handler for it. If that completely unknown code throws an exception and didn't handle itself, you have no guess at all how to restore state. The event handler might have done a bunch of work, mutating state, then died somewhere near the end. You have no hope of un-doing the 'bunch of work' parts, that code is unknown to you. It might have been written months or years after you wrote your code.

    Really Bad Idea, don't do this.

提交回复
热议问题