ReflectionUtils
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace System.Reactive
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal static class ReflectionUtils
{
public static TDelegate CreateDelegate<[System.Runtime.CompilerServices.Nullable(2)] TDelegate>(object o, MethodInfo method)
{
return (TDelegate)(object)method.CreateDelegate(typeof(TDelegate), o);
}
public static Delegate CreateDelegate(Type delegateType, object o, MethodInfo method)
{
return method.CreateDelegate(delegateType, o);
}
public static void GetEventMethods<[System.Runtime.CompilerServices.Nullable(2)] TSender, [System.Runtime.CompilerServices.Nullable(2)] TEventArgs>(Type targetType, [System.Runtime.CompilerServices.Nullable(2)] object target, string eventName, out MethodInfo addMethod, out MethodInfo removeMethod, out Type delegateType, out bool isWinRT)
{
EventInfo event;
if (target == null) {
event = targetType.GetEvent(eventName, BindingFlags.Static | BindingFlags.Public);
if (event == (EventInfo)null)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_STATIC_EVENT, eventName, targetType.FullName));
} else {
event = targetType.GetEvent(eventName, BindingFlags.Instance | BindingFlags.Public);
if (event == (EventInfo)null)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT, eventName, targetType.FullName));
}
MethodInfo addMethod2 = event.GetAddMethod();
if ((object)addMethod2 == null)
throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_ADD_METHOD);
addMethod = addMethod2;
MethodInfo removeMethod2 = event.GetRemoveMethod();
if ((object)removeMethod2 == null)
throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_REMOVE_METHOD);
removeMethod = removeMethod2;
ParameterInfo[] parameters = addMethod.GetParameters();
if (parameters.Length != 1)
throw new InvalidOperationException(Strings_Linq.EVENT_ADD_METHOD_SHOULD_TAKE_ONE_PARAMETER);
ParameterInfo[] parameters2 = removeMethod.GetParameters();
if (parameters2.Length != 1)
throw new InvalidOperationException(Strings_Linq.EVENT_REMOVE_METHOD_SHOULD_TAKE_ONE_PARAMETER);
isWinRT = false;
if (IsWinRTEventRegistrationTokenType(addMethod.ReturnType)) {
isWinRT = true;
if (IsWinRTEventRegistrationTokenType(parameters2[0].ParameterType))
throw new InvalidOperationException(Strings_Linq.EVENT_WINRT_REMOVE_METHOD_SHOULD_TAKE_ERT);
}
delegateType = parameters[0].ParameterType;
MethodInfo method = delegateType.GetMethod("Invoke");
ParameterInfo[] parameters3 = method.GetParameters();
if (parameters3.Length != 2)
throw new InvalidOperationException(Strings_Linq.EVENT_PATTERN_REQUIRES_TWO_PARAMETERS);
if (!typeof(TSender).IsAssignableFrom(parameters3[0].ParameterType))
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_SENDER_NOT_ASSIGNABLE, typeof(TSender).FullName));
if (!typeof(TEventArgs).IsAssignableFrom(parameters3[1].ParameterType))
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.EVENT_ARGS_NOT_ASSIGNABLE, typeof(TEventArgs).FullName));
if (method.ReturnType != typeof(void))
throw new InvalidOperationException(Strings_Linq.EVENT_MUST_RETURN_VOID);
}
private static bool IsWinRTEventRegistrationTokenType(Type t)
{
if (t.Name == "EventRegistrationToken") {
if (!(t.Namespace == "System.Runtime.InteropServices.WindowsRuntime"))
return t.Namespace == "WinRT";
return true;
}
return false;
}
}
}