From 2b49354fd6b91c72a9599b2a2f78b2e72481b71a Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 12 Apr 2018 13:34:32 -0400 Subject: [PATCH] feat: Reflection util for subscribing to an EventInfo --- ICD.Common.Utils/ReflectionUtils.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 1260bb6..d21b9c9 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -414,5 +414,26 @@ namespace ICD.Common.Utils throw new InvalidCastException(message, e); } } + + /// + /// Subscribes to the event on the given instance using the handler and callback method. + /// + /// + /// + /// + /// + /// + public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback) + { + if (eventInfo == null) + throw new ArgumentNullException("eventInfo"); + + if (callback == null) + throw new ArgumentNullException("callback"); + + Delegate output = CreateDelegate(eventInfo.EventHandlerType, handler, callback); + eventInfo.AddEventHandler(instance, output); + return output; + } } }