ReflectionUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. using System.Runtime.InteropServices.WindowsRuntime;
  8. #endif
  9. namespace System.Reactive
  10. {
  11. internal static class ReflectionUtils
  12. {
  13. public static TDelegate CreateDelegate<TDelegate>(object o, MethodInfo method)
  14. {
  15. return (TDelegate)(object)method.CreateDelegate(typeof(TDelegate), o);
  16. }
  17. public static Delegate CreateDelegate(Type delegateType, object o, MethodInfo method)
  18. {
  19. return method.CreateDelegate(delegateType, o);
  20. }
  21. public static void GetEventMethods<TSender, TEventArgs>(Type targetType, object target, string eventName, out MethodInfo addMethod, out MethodInfo removeMethod, out Type delegateType, out bool isWinRT)
  22. {
  23. var e = default(EventInfo);
  24. if (target == null)
  25. {
  26. e = targetType.GetEvent(eventName, BindingFlags.Public | BindingFlags.Static);
  27. if (e == null)
  28. {
  29. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_STATIC_EVENT, eventName, targetType.FullName));
  30. }
  31. }
  32. else
  33. {
  34. e = targetType.GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance);
  35. if (e == null)
  36. {
  37. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT, eventName, targetType.FullName));
  38. }
  39. }
  40. addMethod = e.GetAddMethod();
  41. removeMethod = e.GetRemoveMethod();
  42. if (addMethod == null)
  43. {
  44. throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_ADD_METHOD);
  45. }
  46. if (removeMethod == null)
  47. {
  48. throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_REMOVE_METHOD);
  49. }
  50. var psa = addMethod.GetParameters();
  51. if (psa.Length != 1)
  52. {
  53. throw new InvalidOperationException(Strings_Linq.EVENT_ADD_METHOD_SHOULD_TAKE_ONE_PARAMETER);
  54. }
  55. var psr = removeMethod.GetParameters();
  56. if (psr.Length != 1)
  57. {
  58. throw new InvalidOperationException(Strings_Linq.EVENT_REMOVE_METHOD_SHOULD_TAKE_ONE_PARAMETER);
  59. }
  60. isWinRT = false;
  61. #if HAS_WINRT && !CSWINRT
  62. if (addMethod.ReturnType == typeof(EventRegistrationToken))
  63. {
  64. isWinRT = true;
  65. var pet = psr[0];
  66. if (pet.ParameterType != typeof(EventRegistrationToken))
  67. {
  68. throw new InvalidOperationException(Strings_Linq.EVENT_WINRT_REMOVE_METHOD_SHOULD_TAKE_ERT);
  69. }
  70. }
  71. #endif
  72. delegateType = psa[0].ParameterType;
  73. var invokeMethod = delegateType.GetMethod("Invoke");
  74. var parameters = invokeMethod.GetParameters();
  75. if (parameters.Length != 2)
  76. {
  77. throw new InvalidOperationException(Strings_Linq.EVENT_PATTERN_REQUIRES_TWO_PARAMETERS);
  78. }
  79. if (!typeof(TSender).IsAssignableFrom(parameters[0].ParameterType))
  80. {
  81. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_SENDER_NOT_ASSIGNABLE, typeof(TSender).FullName));
  82. }
  83. if (!typeof(TEventArgs).IsAssignableFrom(parameters[1].ParameterType))
  84. {
  85. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_ARGS_NOT_ASSIGNABLE, typeof(TEventArgs).FullName));
  86. }
  87. if (invokeMethod.ReturnType != typeof(void))
  88. {
  89. throw new InvalidOperationException(Strings_Linq.EVENT_MUST_RETURN_VOID);
  90. }
  91. }
  92. #if (CRIPPLED_REFLECTION && HAS_WINRT)
  93. public static MethodInfo GetMethod(this Type type, string name) => type.GetTypeInfo().GetDeclaredMethod(name);
  94. #endif
  95. }
  96. }