mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
feat: Reflection method for unsubscribing from an event.
This commit is contained in:
@@ -418,10 +418,10 @@ namespace ICD.Common.Utils
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Subscribes to the event on the given instance using the handler and callback method.
|
/// Subscribes to the event on the given instance using the handler and callback method.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="instance"></param>
|
/// <param name="instance">The instance with the event. Null for static types.</param>
|
||||||
/// <param name="eventInfo"></param>
|
/// <param name="eventInfo">The EventInfo for the event.</param>
|
||||||
/// <param name="handler"></param>
|
/// <param name="handler">The instance with the callback MethodInfo. Null for static types.</param>
|
||||||
/// <param name="callback"></param>
|
/// <param name="callback">The MethodInfo for the callback method.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)
|
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)
|
||||||
{
|
{
|
||||||
@@ -435,5 +435,22 @@ namespace ICD.Common.Utils
|
|||||||
eventInfo.AddEventHandler(instance, output);
|
eventInfo.AddEventHandler(instance, output);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unsubscribes from the event on the given instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="instance">The instance with the event. Null for static types.</param>
|
||||||
|
/// <param name="eventInfo">The EventInfo for the event.</param>
|
||||||
|
/// <param name="callback">The Delegate to be removed from the event.</param>
|
||||||
|
public static void UnsubscribeEvent(object instance, EventInfo eventInfo, Delegate callback)
|
||||||
|
{
|
||||||
|
if (eventInfo == null)
|
||||||
|
throw new ArgumentNullException("eventInfo");
|
||||||
|
|
||||||
|
if (callback == null)
|
||||||
|
throw new ArgumentNullException("callback");
|
||||||
|
|
||||||
|
eventInfo.RemoveEventHandler(instance, callback);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user