mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added shims for ReflectionUtils.SubscribeEvent for known callbacks
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
|
using ICD.Common.Utils.EventArguments;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp.Reflection;
|
using Crestron.SimplSharp.Reflection;
|
||||||
#else
|
#else
|
||||||
@@ -149,5 +151,75 @@ namespace ICD.Common.Utils.Tests
|
|||||||
// Everything else
|
// Everything else
|
||||||
Assert.AreEqual(10, ReflectionUtils.ChangeType("10", typeof(int)));
|
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<IntEventArgs>(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<IntEventArgs> OnIncrementCount;
|
||||||
|
|
||||||
|
private int m_Count;
|
||||||
|
|
||||||
|
private void IncrementCount(object sender, IntEventArgs args)
|
||||||
|
{
|
||||||
|
m_Count += args.Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -337,8 +337,9 @@ namespace ICD.Common.Utils
|
|||||||
if (valueType.IsIntegerNumeric())
|
if (valueType.IsIntegerNumeric())
|
||||||
return Enum.ToObject(type, value);
|
return Enum.ToObject(type, value);
|
||||||
|
|
||||||
if (value is string)
|
string valueAsString = value as string;
|
||||||
return Enum.Parse(type, value as string, false);
|
if (valueAsString != null)
|
||||||
|
return Enum.Parse(type, valueAsString, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Convert.ChangeType(value, type, null);
|
return Convert.ChangeType(value, type, null);
|
||||||
@@ -352,12 +353,56 @@ 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 event handler.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="instance">The instance with the event. Null for static types.</param>
|
/// <param name="instance">The instance with the event. Null for static types.</param>
|
||||||
/// <param name="eventInfo">The EventInfo for the event.</param>
|
/// <param name="eventInfo">The EventInfo for the event.</param>
|
||||||
/// <param name="handler">The instance with the callback MethodInfo. Null for static types.</param>
|
/// <param name="eventHandler">The EventHandler for the callback.</param>
|
||||||
/// <param name="callback">The MethodInfo for the callback method.</param>
|
/// <returns></returns>
|
||||||
|
[NotNull]
|
||||||
|
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, Action<object> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Subscribes to the event on the given instance using the event handler.
|
||||||
|
/// </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="eventHandler">The EventHandler for the callback.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[NotNull]
|
||||||
|
public static Delegate SubscribeEvent<T>(object instance, EventInfo eventInfo, Action<object, T> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Subscribes to the event on the given instance using the handler and eventHandler method.
|
||||||
|
/// </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="handler">The instance with the eventHandler MethodInfo. Null for static types.</param>
|
||||||
|
/// <param name="callback">The MethodInfo for the eventHandler method.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[NotNull]
|
[NotNull]
|
||||||
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)
|
public static Delegate SubscribeEvent(object instance, EventInfo eventInfo, object handler, MethodInfo callback)
|
||||||
|
|||||||
Reference in New Issue
Block a user