diff --git a/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs b/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs index 1ba8e18..600df85 100644 --- a/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs +++ b/ICD.Common.Utils.Tests/ReflectionUtilsTest.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using ICD.Common.Properties; +using ICD.Common.Utils.EventArguments; +using ICD.Common.Utils.Extensions; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -149,5 +151,75 @@ namespace ICD.Common.Utils.Tests // Everything else Assert.AreEqual(10, ReflectionUtils.ChangeType("10", typeof(int))); } + + #region Subscription Tests + + [Test] + public void SubscribeEventTest() + { + EventInfo eventInfo = GetType().GetEvent("OnIncrementCount", BindingFlags.Instance | BindingFlags.Public); + + Delegate del = ReflectionUtils.SubscribeEvent(this, eventInfo, IncrementCount); + + Assert.NotNull(del); + + m_Count = 0; + OnIncrementCount.Raise(this, new IntEventArgs(10)); + + Assert.AreEqual(10, m_Count); + + OnIncrementCount = null; + } + + [Test] + public void SubscribeEventMethodInfoTest() + { + EventInfo eventInfo = GetType().GetEvent("OnIncrementCount", BindingFlags.Instance | BindingFlags.Public); + MethodInfo methodInfo = + GetType().GetMethod("IncrementCount", BindingFlags.Instance | BindingFlags.NonPublic); + + Delegate del = ReflectionUtils.SubscribeEvent(this, eventInfo, this, methodInfo); + + Assert.NotNull(del); + + m_Count = 0; + OnIncrementCount.Raise(this, new IntEventArgs(10)); + + Assert.AreEqual(10, m_Count); + + OnIncrementCount = null; + } + + [Test] + public void UnsubscribeEventTest() + { + EventInfo eventInfo = GetType().GetEvent("OnIncrementCount", BindingFlags.Instance | BindingFlags.Public); + MethodInfo methodInfo = + GetType().GetMethod("IncrementCount", BindingFlags.Instance | BindingFlags.NonPublic); + + Delegate del = ReflectionUtils.SubscribeEvent(this, eventInfo, this, methodInfo); + + Assert.NotNull(del); + + m_Count = 0; + + ReflectionUtils.UnsubscribeEvent(this, eventInfo, del); + + OnIncrementCount.Raise(this, new IntEventArgs(10)); + + Assert.AreEqual(0, m_Count); + } + + // Event has to be public + public event EventHandler OnIncrementCount; + + private int m_Count; + + private void IncrementCount(object sender, IntEventArgs args) + { + m_Count += args.Data; + } + + #endregion } } diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index e2a2ddf..542533f 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -337,8 +337,9 @@ namespace ICD.Common.Utils if (valueType.IsIntegerNumeric()) return Enum.ToObject(type, value); - if (value is string) - return Enum.Parse(type, value as string, false); + string valueAsString = value as string; + if (valueAsString != null) + return Enum.Parse(type, valueAsString, false); } return Convert.ChangeType(value, type, null); @@ -352,12 +353,56 @@ namespace ICD.Common.Utils } /// - /// Subscribes to the event on the given instance using the handler and callback method. + /// Subscribes to the event on the given instance using the event handler. /// /// The instance with the event. Null for static types. /// The EventInfo for the event. - /// The instance with the callback MethodInfo. Null for static types. - /// The MethodInfo for the callback method. + /// The EventHandler for the callback. + /// + [NotNull] + public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, Action eventHandler) + { + if (eventInfo == null) + throw new ArgumentNullException("eventInfo"); + + if (eventHandler == null) + throw new ArgumentNullException("eventHandler"); + + object handler = eventHandler.Target; + MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler); + + return SubscribeEvent(instance, eventInfo, handler, callback); + } + + /// + /// Subscribes to the event on the given instance using the event handler. + /// + /// The instance with the event. Null for static types. + /// The EventInfo for the event. + /// The EventHandler for the callback. + /// + [NotNull] + public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, Action eventHandler) + { + if (eventInfo == null) + throw new ArgumentNullException("eventInfo"); + + if (eventHandler == null) + throw new ArgumentNullException("eventHandler"); + + object handler = eventHandler.Target; + MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler); + + return SubscribeEvent(instance, eventInfo, handler, callback); + } + + /// + /// Subscribes to the event on the given instance using the handler and eventHandler method. + /// + /// The instance with the event. Null for static types. + /// The EventInfo for the event. + /// The instance with the eventHandler MethodInfo. Null for static types. + /// The MethodInfo for the eventHandler method. /// [NotNull] public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)