Browse Source

improve NetCore compatibility

tags/Rel1
sebas77 7 years ago
parent
commit
19472a94c6
5 changed files with 39 additions and 22 deletions
  1. +4
    -0
      Utilities/Console.cs
  2. +24
    -0
      Utilities/PropertyInfoExtensions.cs
  3. +3
    -15
      WeakEvents/WeakAction.cs
  4. +5
    -4
      WeakEvents/WeakActionStruct.cs
  5. +3
    -3
      WeakEvents/WeakEvent.cs

+ 4
- 0
Utilities/Console.cs View File

@@ -1,5 +1,9 @@
using System;
#if NETFX_CORE
using Windows.System.Diagnostics;
#else
using System.Diagnostics;
#endif
using System.Text;

namespace Utility


+ 24
- 0
Utilities/PropertyInfoExtensions.cs View File

@@ -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
}
}

+ 3
- 15
WeakEvents/WeakAction.cs View File

@@ -7,11 +7,7 @@ namespace Svelto.WeakEvents
public class WeakAction<T1, T2> : WeakAction
{
public WeakAction(Action<T1, T2> 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<T> : WeakActionBase
{
public WeakAction(Action<T> 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)


+ 5
- 4
WeakEvents/WeakActionStruct.cs View File

@@ -10,7 +10,7 @@ namespace Svelto.WeakEvents
{
public WeakActionStruct(Action<T1, T2> 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<T> 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


+ 3
- 3
WeakEvents/WeakEvent.cs View File

@@ -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<T1> operator-(WeakEvent<T1> c1, Action<T1> 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<T1, T2> operator-(WeakEvent<T1, T2> c1, Action<T1, T2> x)
{
DesignByContract.Check.Require(x != null);
c1.Remove(x.Target, x.Method);
c1.Remove(x.Target, x.GetMethodInfoEx());

return c1;
}


Loading…
Cancel
Save