As pseudo idea:
C#5 <
class MyDelegateHelperClass{
public static void RemoveEventHandlers<TModel, TItem>(MulticastDelegate m, Expression<Func<TModel, TItem>> expr) {
EventInfo eventInfo= ((MemberExpression)expr.Body).Member as EventInfo;
Delegate[] subscribers = m.GetInvocationList();
Delegate currentDelegate;
for (int i = 0; i < subscribers.Length; i++) {
currentDelegate=subscribers[i];
eventInfo.RemoveEventHandler(currentDelegate.Target,currentDelegate);
}
}
}
Usage:
MyDelegateHelperClass.RemoveEventHandlers(MyDelegate,()=>myClass.myDelegate);
C#6
public static void RemoveEventHandlers(this MulticastDelegate m){
string eventName=nameof(m);
EventInfo eventInfo=m.GetType().ReflectingType.GetEvent(eventName,BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
Delegate[] subscribers = m.GetInvocationList();
Delegate currentDelegate;
for (int i = 0; i < subscribers.Length; i++) {
currentDelegate=subscribers[i];
eventInfo.RemoveEventHandler(currentDelegate.Target,currentDelegate);
}
}
Usage:
MyDelegate.RemoveEventHandlers();