1
0

ReflectionUtils.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Globalization;
  5. using System.Reflection;
  6. #if HAS_WINRT
  7. #if CRIPPLED_REFLECTION
  8. using System.Collections.Generic;
  9. #endif
  10. using System.Runtime.InteropServices.WindowsRuntime;
  11. #endif
  12. namespace System.Reactive
  13. {
  14. internal 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. public static Delegate CreateDelegate(Type delegateType, object o, MethodInfo method)
  25. {
  26. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  27. return method.CreateDelegate(delegateType, o);
  28. #else
  29. return Delegate.CreateDelegate(delegateType, o, method);
  30. #endif
  31. }
  32. public static void GetEventMethods<TSender, TEventArgs>(Type targetType, object target, string eventName, out MethodInfo addMethod, out MethodInfo removeMethod, out Type delegateType, out bool isWinRT)
  33. {
  34. var e = default(EventInfo);
  35. if (target == null)
  36. {
  37. e = targetType.GetEventEx(eventName, true);
  38. if (e == null)
  39. {
  40. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_STATIC_EVENT, eventName, targetType.FullName));
  41. }
  42. }
  43. else
  44. {
  45. e = targetType.GetEventEx(eventName, false);
  46. if (e == null)
  47. {
  48. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT, eventName, targetType.FullName));
  49. }
  50. }
  51. addMethod = e.GetAddMethod();
  52. removeMethod = e.GetRemoveMethod();
  53. if (addMethod == null)
  54. {
  55. throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_ADD_METHOD);
  56. }
  57. if (removeMethod == null)
  58. {
  59. throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_REMOVE_METHOD);
  60. }
  61. var psa = addMethod.GetParameters();
  62. if (psa.Length != 1)
  63. {
  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. {
  69. throw new InvalidOperationException(Strings_Linq.EVENT_REMOVE_METHOD_SHOULD_TAKE_ONE_PARAMETER);
  70. }
  71. isWinRT = false;
  72. #if HAS_WINRT
  73. if (addMethod.ReturnType == typeof(EventRegistrationToken))
  74. {
  75. isWinRT = true;
  76. var pet = psr[0];
  77. if (pet.ParameterType != typeof(EventRegistrationToken))
  78. {
  79. throw new InvalidOperationException(Strings_Linq.EVENT_WINRT_REMOVE_METHOD_SHOULD_TAKE_ERT);
  80. }
  81. }
  82. #endif
  83. delegateType = psa[0].ParameterType;
  84. var invokeMethod = delegateType.GetMethod("Invoke");
  85. var parameters = invokeMethod.GetParameters();
  86. if (parameters.Length != 2)
  87. {
  88. throw new InvalidOperationException(Strings_Linq.EVENT_PATTERN_REQUIRES_TWO_PARAMETERS);
  89. }
  90. if (!typeof(TSender).IsAssignableFrom(parameters[0].ParameterType))
  91. {
  92. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_SENDER_NOT_ASSIGNABLE, typeof(TSender).FullName));
  93. }
  94. if (!typeof(TEventArgs).IsAssignableFrom(parameters[1].ParameterType))
  95. {
  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. {
  100. throw new InvalidOperationException(Strings_Linq.EVENT_MUST_RETURN_VOID);
  101. }
  102. }
  103. public static EventInfo GetEventEx(this Type type, string name, bool isStatic)
  104. {
  105. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  106. // TODO: replace in the future by System.Reflection.RuntimeExtensions extension methods
  107. var q = new Queue<TypeInfo>();
  108. q.Enqueue(type.GetTypeInfo());
  109. while (q.Count > 0)
  110. {
  111. var t = q.Dequeue();
  112. var e = t.GetDeclaredEvent(name);
  113. if (e != null)
  114. {
  115. return e;
  116. }
  117. foreach (var i in t.ImplementedInterfaces)
  118. {
  119. q.Enqueue(i.GetTypeInfo());
  120. }
  121. if (t.BaseType != null)
  122. {
  123. q.Enqueue(t.BaseType.GetTypeInfo());
  124. }
  125. }
  126. return null;
  127. #else
  128. return type.GetEvent(name, isStatic ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.Instance);
  129. #endif
  130. }
  131. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  132. public static MethodInfo GetMethod(this Type type, string name) => type.GetTypeInfo().GetDeclaredMethod(name);
  133. public static MethodInfo GetAddMethod(this EventInfo eventInfo) => eventInfo.AddMethod;
  134. public static MethodInfo GetRemoveMethod(this EventInfo eventInfo) => eventInfo.RemoveMethod;
  135. public static bool IsAssignableFrom(this Type type1, Type type2) => type1.GetTypeInfo().IsAssignableFrom(type2.GetTypeInfo());
  136. #endif
  137. }
  138. }