10

Assume I have a class like so:

public class MyClass<T>
{
    public void Foo(T t)
    {
    }
}

Now, assume, I have an instance of MyClass<int> and a MethodInfo of its Foo method. Calling methodInfo.GetParameters() will return a ParameterInfo array with one entry, referring to type int. My problem is, that I can't seem to find out, if that parameter was declared as int in the class or as T.

What am I trying to achieve?
At runtime, I want to read the documentation of the method specified by MethodInfo from the XML Doc file generated by Visual Studio.
For the above defined method, the key looks like this:

<namespace>.MyClass`1.Foo(`0)

The `0 refers to the first generic type parameter of the declaring class. To be able to construct this string, I need to somehow get this information.
But how? MethodInfo doesn't seem to contain that info...

4
  • Advanced reflection techniques usually requires falling back to IMetaDataImport2. Not that easy to use from C#. Oct 28, 2012 at 15:06
  • @HansPassant: Would this interface support my scenario? Oct 28, 2012 at 19:55
  • So if your method was "public void Foo(int i, T t, string s)", you would want to get something like "<namespace>.MyClass1.Foo(int, 0, string)"?
    – user276648
    Nov 20, 2012 at 3:00
  • See stackoverflow.com/a/4103366/82959 for the easy way to do it.
    – kvb
    Dec 11, 2015 at 17:21

4 Answers 4

3

The key seems to be Type.ContainsGenericParameters on the parameter type:

Given

public class MyClass<T>
{
    public void Foo(T t)
    {
    }

    public void Bar(int i)
    {

    }
}

Then

class Program
{
    static void Main(string[] args)
    {
        var obj = new MyClass<int>();

        // Closed type
        var closedType = obj.GetType();

        // Open generic (typeof(MyClass<>))
        var openType = closedType.GetGenericTypeDefinition();

        // Methods on open type
        var fooT = openType.GetMethod("Foo");
        var barint = openType.GetMethod("Bar");

        // Parameter types
        var tInFoo = fooT.GetParameters()[0].ParameterType;
        var iInBar = barint.GetParameters()[0].ParameterType;

        // Are they generic?
        var tInFooIsGeneric = tInFoo.ContainsGenericParameters;
        var iInBarIsGeneric = iInBar.ContainsGenericParameters;

        Console.WriteLine(tInFooIsGeneric);
        Console.WriteLine(iInBarIsGeneric);

        Console.ReadKey();
    }
}

outputs

True
False

This will obviously need more work for overloads and so on.

4
  • Aha! That looks pretty good. Will check it later in detail. Thanks! Nov 22, 2012 at 11:24
  • Sorry for taking so long to accept your answer. It was spot on. Jul 24, 2013 at 13:48
  • 1
    How can I get the correct MethodInfo if I have overloads of the Foo method? GetMethod throws an AmbiguousMatchException. GetMethod needs to receive the parameters type but I can't find the generic parameter from the MethodInfo closed type Sep 9, 2014 at 13:44
  • @AlexandrePepin then use Type.GetMethods with a Where(mi => mi.Name="Whatever"), then further filters to pick the one you want. Ask your own question if you need more after you've tried this...
    – AakashM
    Sep 9, 2014 at 13:53
2

This was a tricky one. If you have a MethodInfo instance of a (non-generic) method, which was obtained on an open generic Type, you can actually use it to repeatedly close the method (i.e., with different generic arguments for the enclosing type).

The key is to use the obscure static function MethodInfo.GetMethodFromHandle(...):

Complete working example:

static class MyType<T>
{
    public static int TheMethod() => typeof(T).MetadataToken;  // demo method
};

How to use:

static void demo()
{
    var Topen = typeof(MyType<>);
    var mi_open = Topen.GetMethod("TheMethod");  // can't Invoke() this one...

    // ...later perhaps...  MyType<ushort>.TheMethod()

    var Tclose = Topen.MakeGenericType(typeof(ushort));
    var mi = MethodInfo.GetMethodFromHandle(mi_open.MethodHandle, Tclose.TypeHandle);
    var ret = (int)mi.Invoke(null, null);        // works --> 0x02000150

    // later yet... MyType<Guid>.TheMethod(), reusing 'mi_open' (and Topen)...

    Tclose = Topen.MakeGenericType(typeof(Guid));
    mi = MethodInfo.GetMethodFromHandle(mi_open.MethodHandle, Tclose.TypeHandle);
    ret = (int)mi.Invoke(null, null);            // works --> 0x020000eb
}

Notice that the Tclose type handle (passed in as the second arg) is not simply the handle of the type argument T that you ultimately want to parametrize with. Rather, you have to use that T to close the original Topen (open) generic type first, and then use the handle for the resulting (closed) generic type.

In other words, you can't close a method without first explicitly closing its generic type.

1
  • I don't know why this hasn't got more upvotes!
    – Steve Dunn
    Apr 19, 2022 at 9:42
1

Could you get the definition of the generic class through Type.GetGenericTypeDefinition Method and find there the definition for the same method, say, by name (and the signature), and then compare Foo(T t) and Foo(int t):

MyClass<int> c = new MyClass<int>();

Type concreteType = c.GetType();
Console.Write("Concrete type name:");
Console.WriteLine(concreteType.FullName);
Console.WriteLine();

MethodInfo concreteMethod = concreteType.GetMethod("Foo");
if (concreteMethod != null)
{
    Console.WriteLine(concreteMethod.Name);
    foreach (ParameterInfo pinfo in concreteMethod.GetParameters())
    {
        Console.WriteLine(pinfo.Name);
        Console.WriteLine(pinfo.ParameterType);
        Console.WriteLine();
    }
    Console.WriteLine();
}

if (concreteType.IsGenericType)
{
    Console.Write("Generic type name:");
    Type genericType = concreteType.GetGenericTypeDefinition();
    Console.WriteLine(genericType.FullName);
    Console.WriteLine();

    MethodInfo genericMethod = genericType.GetMethod("Foo");
    if (genericMethod != null)
    {
        Console.WriteLine(genericMethod.Name);
        foreach (ParameterInfo pinfo in genericMethod.GetParameters())
        {
            Console.WriteLine(pinfo.Name);
            Console.WriteLine(pinfo.ParameterType);
            Console.WriteLine();
        }
        Console.WriteLine();
    }
}
2
  • I thought about that too, but I am not really sure how that would work out with overloaded methods. Oct 28, 2012 at 12:52
  • @DanielHilgarth, agree that could be a tricky thing to find correspondent methods' overloads. Yet I don't see the other way out either.
    – horgh
    Oct 28, 2012 at 13:16
1

I don't know if you have considered using Mono.Cecil instead of .Net's reflection.

// Gets the AssemblyDefinition (similar to .Net's Assembly).
Type testType = typeof(MyClass<>);
AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(new Uri(testType.Assembly.CodeBase).LocalPath);
// Gets the TypeDefinition (similar to .Net's Type).
TypeDefinition classDef = assemblyDef.MainModule.Types.Single(typeDef => typeDef.Name == testType.Name);
// Gets the MethodDefinition (similar to .Net's MethodInfo).
MethodDefinition myMethodDef = classDef.Methods.Single(methDef => methDef.Name == "Foo");

then myMethodDef.FullName returns

"System.Void MyNamespace.MyClass`1::Foo(System.Int32,T,System.String)"

and classDef.GenericParameters[0].FullName returns

"T"

Note that Mono.Cecil uses a different way of writing generics, nested classes and arrays:

List[T] => List<T>
MyClass+MyNestedClass => MyClass/MyNestedClass
int[,] => int[0...,0...]
1
  • The problem is that I already have a MethodInfo object for which I need to retrieve that info. Nov 22, 2012 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.