Calling generic method with Type variable [duplicate]

Lets assume that Foo is declared in class Test such as

public class Test
{
   public void Foo<T>() { ... }

}

You need to first instantiate the method for type bar using MakeGenericMethod. And then invoke it using reflection.

var mi = typeof(Test).GetMethod("Foo");
var fooRef = mi.MakeGenericMethod(bar);
fooRef.Invoke(new Test(), null);

Leave a Comment