jeudi 25 juin 2015

Check if method return type is based on generic parameter

When I have a MethodInfo how can I check if the return type is based on a generic parameter?

public class GenericClass<T>
{
  public class InnerClass 
  {
    public static T A()
    {
      return default(T);
    }
  }       
}

When I check

typeof(GenericClass<>.InnerClass).GetMethod("A").ReturnType.IsGenericParameter

I get true but what if I only have a (closed) MethodInfo of a GenericClass<int> for example? Do I have to walk up the path of nested types and check if one has IsGeneric == true then get the GetGenericTypeDefinition of this type then walk the path down by instantiating open Types until I can create an open version of the MethodInfo or is there an easier (and faster) way?





Aucun commentaire:

Enregistrer un commentaire