ReflectionUtils.cs 5.6 KB

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