From 19472a94c6014d116ba48aa396913b7b5609b4a0 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Wed, 8 Nov 2017 10:32:02 +0000 Subject: [PATCH] improve NetCore compatibility --- Utilities/Console.cs | 4 ++++ Utilities/PropertyInfoExtensions.cs | 24 ++++++++++++++++++++++++ WeakEvents/WeakAction.cs | 18 +++--------------- WeakEvents/WeakActionStruct.cs | 9 +++++---- WeakEvents/WeakEvent.cs | 6 +++--- 5 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 Utilities/PropertyInfoExtensions.cs diff --git a/Utilities/Console.cs b/Utilities/Console.cs index db80a6c..cf11720 100644 --- a/Utilities/Console.cs +++ b/Utilities/Console.cs @@ -1,5 +1,9 @@ using System; +#if NETFX_CORE +using Windows.System.Diagnostics; +#else using System.Diagnostics; +#endif using System.Text; namespace Utility diff --git a/Utilities/PropertyInfoExtensions.cs b/Utilities/PropertyInfoExtensions.cs new file mode 100644 index 0000000..87b18d8 --- /dev/null +++ b/Utilities/PropertyInfoExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Reflection; + +public static class NetFXCoreWrappers +{ + public static MethodInfo GetMethodInfoEx(this Delegate delegateEx) + { +#if NETFX_CORE + var method = delegateEx.GetMethodInfo(); +#else + var method = delegateEx.Method; +#endif + return method; + } + + public static Type GetDeclaringType(this MethodInfo methodInfo) + { +#if NETFX_CORE + return methodInfo.DeclaringType; +#else + return methodInfo.ReflectedType; +#endif + } +} diff --git a/WeakEvents/WeakAction.cs b/WeakEvents/WeakAction.cs index 2889e70..8c5ece6 100644 --- a/WeakEvents/WeakAction.cs +++ b/WeakEvents/WeakAction.cs @@ -7,11 +7,7 @@ namespace Svelto.WeakEvents public class WeakAction : WeakAction { public WeakAction(Action listener) -#if NETFX_CORE - : base(listener.Target, listener.GetMethodInfo()) -#else - : base(listener.Target, listener.Method) -#endif + : base(listener.Target, listener.GetMethodInfoEx()) {} public void Invoke(T1 data1, T2 data2) @@ -28,11 +24,7 @@ namespace Svelto.WeakEvents public class WeakAction : WeakActionBase { public WeakAction(Action listener) -#if NETFX_CORE - : base(listener.Target, listener.GetMethodInfo()) -#else - : base(listener.Target, listener.Method) -#endif + : base(listener.Target, listener.GetMethodInfoEx()) {} public void Invoke(T data) @@ -70,11 +62,7 @@ namespace Svelto.WeakEvents } protected WeakActionBase(Action listener) -#if NETFX_CORE - : this(listener.Target, listener.GetMethodInfo()) -#else - : this(listener.Target, listener.Method) -#endif + : this(listener.Target, listener.GetMethodInfoEx()) {} protected WeakActionBase(object listener, MethodInfo method) diff --git a/WeakEvents/WeakActionStruct.cs b/WeakEvents/WeakActionStruct.cs index d3b37a9..7686934 100644 --- a/WeakEvents/WeakActionStruct.cs +++ b/WeakEvents/WeakActionStruct.cs @@ -10,7 +10,7 @@ namespace Svelto.WeakEvents { public WeakActionStruct(Action listener) { - WeakActionStructUtility.Init(listener.Target, listener.Method, out _objectRef, out _method); + WeakActionStructUtility.Init(listener.Target, listener.GetMethodInfoEx(), out _objectRef, out _method); } public bool Invoke(object[] args) @@ -43,7 +43,7 @@ namespace Svelto.WeakEvents { public WeakActionStruct(Action listener) { - WeakActionStructUtility.Init(listener.Target, listener.Method, + WeakActionStructUtility.Init(listener.Target, listener.GetMethodInfoEx(), out _objectRef, out _method); } @@ -77,7 +77,8 @@ namespace Svelto.WeakEvents { public WeakActionStruct(Action listener) { - WeakActionStructUtility.Init(listener.Target, listener.Method, out _objectRef, out _method); + WeakActionStructUtility.Init(listener.Target, listener.GetMethodInfoEx(), + out _objectRef, out _method); } public bool Invoke() @@ -121,7 +122,7 @@ namespace Svelto.WeakEvents if(attributes.Length != 0) throw new ArgumentException("Cannot create weak event to anonymous method with closure."); #else - if (method.DeclaringType.GetCustomAttributes(typeof (CompilerGeneratedAttribute), false).Length != 0) + if (method.DeclaringType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length != 0) throw new ArgumentException("Cannot create weak event to anonymous method with closure."); #endif #endif diff --git a/WeakEvents/WeakEvent.cs b/WeakEvents/WeakEvent.cs index 84a8b53..485df35 100644 --- a/WeakEvents/WeakEvent.cs +++ b/WeakEvents/WeakEvent.cs @@ -17,7 +17,7 @@ namespace Svelto.WeakEvents public static WeakEvent operator-(WeakEvent c1, Action x) { DesignByContract.Check.Require(x != null); - c1.Remove(x.Target, x.Method); + c1.Remove(x.Target, x.GetMethodInfoEx()); return c1; } @@ -66,7 +66,7 @@ namespace Svelto.WeakEvents public static WeakEvent operator-(WeakEvent c1, Action x) { DesignByContract.Check.Require(x != null); - c1.Remove(x.Target, x.Method); + c1.Remove(x.Target, x.GetMethodInfoEx()); return c1; } @@ -119,7 +119,7 @@ namespace Svelto.WeakEvents public static WeakEvent operator-(WeakEvent c1, Action x) { DesignByContract.Check.Require(x != null); - c1.Remove(x.Target, x.Method); + c1.Remove(x.Target, x.GetMethodInfoEx()); return c1; }