ReflectionUtils.cs 5.6 KB

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