ReflectionUtils.cs 5.8 KB

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