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

后端 未结 3 2101
逝去的感伤
逝去的感伤 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:45

    Is the type of the delegate fixed? You'll need to either use quite a bit of reflection, or you need to implement one version for each possible parametercount just like there is one Action<...> type for it.

    Should look similar to this(untested notepad code):

      public static Action WrapAction(Action a)
      {
       var invList = ((MultiCastDelegate)a).GetInvocationList();
    
       for (int i = 0; i < invList.Length; i++)
       {
        invList[i] = ()=>{try invList[i] catch {...} });
       }
       return (Action)MulticastDelegate.Combine(invList);
      }
    

    And you probably need to add special case handling for single cast delegates.

提交回复
热议问题