| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
using System.Collections.Generic; |
| 5 |
using System.Globalization; |
| 6 |
using System.Reflection; |
| 7 |
|
| 8 |
#if HAS_WINRT |
| 9 |
using System.Runtime.InteropServices.WindowsRuntime; |
| 10 |
#endif |
| 11 |
|
| 12 |
namespace System.Reactive |
| 13 |
{ |
| 14 |
static class ReflectionUtils |
| 15 |
{ |
| 16 |
public static TDelegate CreateDelegate<TDelegate>(object o, MethodInfo method) |
| 17 |
{ |
| 18 |
#if (CRIPPLED_REFLECTION && HAS_WINRT) |
| 19 |
return (TDelegate)(object)method.CreateDelegate(typeof(TDelegate), o); |
| 20 |
#else |
| 21 |
return (TDelegate)(object)Delegate.CreateDelegate(typeof(TDelegate), o, method); |
| 22 |
#endif |
| 23 |
} |
| 24 |
|
| 25 |
public static Delegate CreateDelegate(Type delegateType, object o, MethodInfo method) |
| 26 |
{ |
| 27 |
#if (CRIPPLED_REFLECTION && HAS_WINRT) |
| 28 |
return method.CreateDelegate(delegateType, o); |
| 29 |
#else |
| 30 |
return Delegate.CreateDelegate(delegateType, o, method); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
public static void GetEventMethods<TSender, TEventArgs>(Type targetType, object target, string eventName, out MethodInfo addMethod, out MethodInfo removeMethod, out Type delegateType, out bool isWinRT) |
| 35 |
#if !NO_EVENTARGS_CONSTRAINT |
| 36 |
where TEventArgs : EventArgs |
| 37 |
#endif |
| 38 |
{ |
| 39 |
var e = default(EventInfo); |
| 40 |
|
| 41 |
if (target == null) |
| 42 |
{ |
| 43 |
e = targetType.GetEventEx(eventName, true); |
| 44 |
if (e == null) |
| 45 |
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_STATIC_EVENT, eventName, targetType.FullName)); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
e = targetType.GetEventEx(eventName, false); |
| 50 |
if (e == null) |
| 51 |
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT, eventName, targetType.FullName)); |
| 52 |
} |
| 53 |
|
| 54 |
addMethod = e.GetAddMethod(); |
| 55 |
removeMethod = e.GetRemoveMethod(); |
| 56 |
|
| 57 |
if (addMethod == null) |
| 58 |
throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_ADD_METHOD); |
| 59 |
if (removeMethod == null) |
| 60 |
throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_REMOVE_METHOD); |
| 61 |
|
| 62 |
var psa = addMethod.GetParameters(); |
| 63 |
if (psa.Length != 1) |
| 64 |
throw new InvalidOperationException(Strings_Linq.EVENT_ADD_METHOD_SHOULD_TAKE_ONE_PARAMETER); |
| 65 |
|
| 66 |
var psr = removeMethod.GetParameters(); |
| 67 |
if (psr.Length != 1) |
| 68 |
throw new InvalidOperationException(Strings_Linq.EVENT_REMOVE_METHOD_SHOULD_TAKE_ONE_PARAMETER); |
| 69 |
|
| 70 |
isWinRT = false; |
| 71 |
|
| 72 |
#if HAS_WINRT |
| 73 |
if (addMethod.ReturnType == typeof(EventRegistrationToken)) |
| 74 |
{ |
| 75 |
isWinRT = true; |
| 76 |
|
| 77 |
var pet = psr[0]; |
| 78 |
if (pet.ParameterType != typeof(EventRegistrationToken)) |
| 79 |
throw new InvalidOperationException(Strings_Linq.EVENT_WINRT_REMOVE_METHOD_SHOULD_TAKE_ERT); |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
delegateType = psa[0].ParameterType; |
| 84 |
|
| 85 |
var invokeMethod = delegateType.GetMethod("Invoke"); |
| 86 |
|
| 87 |
var parameters = invokeMethod.GetParameters(); |
| 88 |
|
| 89 |
if (parameters.Length != 2) |
| 90 |
throw new InvalidOperationException(Strings_Linq.EVENT_PATTERN_REQUIRES_TWO_PARAMETERS); |
| 91 |
|
| 92 |
if (!typeof(TSender).IsAssignableFrom(parameters[0].ParameterType)) |
| 93 |
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_SENDER_NOT_ASSIGNABLE, typeof(TSender).FullName)); |
| 94 |
|
| 95 |
if (!typeof(TEventArgs).IsAssignableFrom(parameters[1].ParameterType)) |
| 96 |
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_ARGS_NOT_ASSIGNABLE, typeof(TEventArgs).FullName)); |
| 97 |
|
| 98 |
if (invokeMethod.ReturnType != typeof(void)) |
| 99 |
throw new InvalidOperationException(Strings_Linq.EVENT_MUST_RETURN_VOID); |
| 100 |
} |
| 101 |
|
| 102 |
public static EventInfo GetEventEx(this Type type, string name, bool isStatic) |
| 103 |
{ |
| 104 |
#if (CRIPPLED_REFLECTION && HAS_WINRT) |
| 105 |
// TODO: replace in the future by System.Reflection.RuntimeExtensions extension methods |
| 106 |
var q = new Queue<TypeInfo>(); |
| 107 |
q.Enqueue(type.GetTypeInfo()); |
| 108 |
|
| 109 |
while (q.Count > 0) |
| 110 |
{ |
| 111 |
var t = q.Dequeue(); |
| 112 |
|
| 113 |
var e = t.GetDeclaredEvent(name); |
| 114 |
if (e != null) |
| 115 |
return e; |
| 116 |
|
| 117 |
foreach (var i in t.ImplementedInterfaces) |
| 118 |
q.Enqueue(i.GetTypeInfo()); |
| 119 |
|
| 120 |
if (t.BaseType != null) |
| 121 |
q.Enqueue(t.BaseType.GetTypeInfo()); |
| 122 |
} |
| 123 |
|
| 124 |
return null; |
| 125 |
#else |
| 126 |
return type.GetEvent(name, isStatic ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.Instance); |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
#if (CRIPPLED_REFLECTION && HAS_WINRT) |
| 131 |
public static MethodInfo GetMethod(this Type type, string name) |
| 132 |
{ |
| 133 |
return type.GetTypeInfo().GetDeclaredMethod(name); |
| 134 |
} |
| 135 |
|
| 136 |
public static MethodInfo GetAddMethod(this EventInfo eventInfo) |
| 137 |
{ |
| 138 |
return eventInfo.AddMethod; |
| 139 |
} |
| 140 |
|
| 141 |
public static MethodInfo GetRemoveMethod(this EventInfo eventInfo) |
| 142 |
{ |
| 143 |
return eventInfo.RemoveMethod; |
| 144 |
} |
| 145 |
|
| 146 |
public static bool IsAssignableFrom(this Type type1, Type type2) |
| 147 |
{ |
| 148 |
return type1.GetTypeInfo().IsAssignableFrom(type2.GetTypeInfo()); |
| 149 |
} |
| 150 |
#endif |
| 151 |
} |
| 152 |
} |