Home > Software engineering >  Type[] for GetMethod to get generic method with two arguments and one type parameter
Type[] for GetMethod to get generic method with two arguments and one type parameter

Time:12-20

Exist many variants how to receive the generic method: through search in list of all methods (Type.GetMethods()) by LINQ and etc or by creation the delegate as method template. But interesting why it doesn't work the classic GetMethod() by Reflection. We main problem in this case is to create the right Type[] with the list of demanded method arguments (method signature). And I can understand is it a limitation of c# or have other explanation in this example? Initially we have a class

public class MyClass 
{
    public static void AddLink()
    {
        Console.WriteLine("Hello from AddLink()");
    }
    public static void AddLink<T0>(UnityAction<T0> unityAction, UnityEvent<T0> unityEvent)
    {
        unityEvent.AddListener(unityAction);
    }
    public static void AddLink<T0, T1>(UnityAction<T0, T1> unityAction, UnityEvent<T0, T1> unityEvent)
    {
        unityEvent.AddListener(unityAction);
    }
}

and we want to get the method void AddLink<T0>(UnityAction<T0> unityAction, UnityEvent<T0> unityEvent) by use the MethodInfo method = typeof(MyClass).GetMethod("AddLink", typeParameters). I tested different variants of typeParameters

Type[] typeParameters = new Type[] {typeof(UnityAction<>), typeof(UnityEvent<>)};
Type[] typeParametersClosed = new Type[] { typeof(UnityAction<bool>), typeof(UnityEvent<bool>) };
Type[] typeParametersClosedGeneric = new Type[] { typeof(UnityAction<bool>).GetGenericTypeDefinition(), typeof(UnityEvent<bool>).GetGenericTypeDefinition()};

Nobody gave a result. I could found the method by searching in GetMthods() or by casting a delegate to demanding type:

var template = (Action<UnityAction<object>, UnityEvent<object>>)(MyClass.AddLink);
MethodInfo methodGeneric = template.Method.GetGenericMethodDefinition();

and for testing I decided to get parameters from founded method

Type[] typeParametersFromGeneric = GetParametersFromMethodInfo(methodGeneric);

public static Type[] GetParametersFromMethodInfo(MethodInfo method)
{
    ParameterInfo[] parameterInfo = method.GetParameters();
    int length = parameterInfo.Length;
    Type[] parameters = new Type[length];
    for (int i = 0; i < length; i  )
    {
        parameters[i] = parameterInfo[i].ParameterType;
    }
    return parameters;
}

:) And after that, use the final Type[] (typeParametersFromGeneric) the GetMethod began working.

I compared all these Type[] (i removed here the info from second argument, it was the same):

enter image description here

The Main Question is it possible to create the Type[] (typeParametersFromGeneric) from scratch? and why it it isn't possible

CodePudding user response:

You can use Type.MakeGenericSignatureType and pass Type.MakeGenericMethodParameter(0) to it as generic parameters:

var methodInfo = typeof(MyClass)
    .GetMethod(nameof(MyClass.AddLink), new[]
    {
        Type.MakeGenericSignatureType(typeof(UnityAction<>), Type.MakeGenericMethodParameter(0)),
        Type.MakeGenericSignatureType(typeof(UnityEvent<>), Type.MakeGenericMethodParameter(0))
    });

Demo

  • Related