You can invoke a generic method via reflection if you have it's MethodInfo
.
First call that MethodInfo
's MethodInfo MakeGenericMethod(params Type[] typeArguments)
with one type for each generic type argument to the method.
Then call the resulting MethodInfo
's object Invoke(object obj, object[] parameters)
with the instance of the object you want the method invoked on (or null
if it is a static
method), and an array of objects containing the arguments to the method in order.
Here is your c# code modified to compile, run, and do something. Example1()
is your first code snippet. Example2()
does what you want your second code snippet to do.
public class SomeBaseClass {
public string someProperty {
set {
Console.WriteLine(string.Format("someProperty called on {0} with {1}", this, value ) );
}
}
}
public class Foo : SomeBaseClass {
}
public class Bar : SomeBaseClass {
}
public class Baz : SomeBaseClass {
}
public static class SomeMethods {
public static T SomeMethod<T>() where T : SomeBaseClass, new() {
return new T();
}
}
class Program
{
public static void Example1() {
string someValue = "called from Example1";
SomeMethods.SomeMethod<Foo>().someProperty = someValue;
SomeMethods.SomeMethod<Bar>().someProperty = someValue;
SomeMethods.SomeMethod<Baz>().someProperty = someValue;
}
public static void Example2() {
string someValue = "called from Example2";
Type[] types = new Type[]{
typeof(Foo), typeof(Bar), typeof(Baz), //...
};
foreach (Type type in types) {
// Here's how:
System.Reflection.MethodInfo genericMethodInfo = typeof(SomeMethods).GetMethod("SomeMethod");
System.Reflection.MethodInfo methodInfoForType = genericMethodInfo.MakeGenericMethod(type);
var someBase = (SomeBaseClass) methodInfoForType.Invoke(null, new object[] { });
someBase.someProperty = someValue;
}
}
static void Main(string[] args)
{
Console.WriteLine("Example1");
Example1();
Console.WriteLine("Example2");
Example2();
Console.ReadKey();
}
}
Here's the output of the program:
Example1
someProperty called on ConsoleApplication1.Foo with called from Example1
someProperty called on ConsoleApplication1.Bar with called from Example1
someProperty called on ConsoleApplication1.Baz with called from Example1
Example2
someProperty called on ConsoleApplication1.Foo with called from Example2
someProperty called on ConsoleApplication1.Bar with called from Example2
someProperty called on ConsoleApplication1.Baz with called from Example2