class GenericClass<T1, T2> {
public T1 SomeComplexMethod<T3>(T2 inputA, T3 inputB) {
// ...
}
}
class Program {
static void Main() {
var gc = new GenericClass<int, string>();
var resultA = InvokeComplexMethodReflectively(gc, "Hi", 3f);
var resultB = InvokeComplexMethodDynamically(gc, "Hi", 3f);
Console.WriteLine(resultA);
Console.WriteLine(resultB);
}
static object InvokeComplexMethodReflectively(object genericClassInstance, string inputA, float inputB) {
var openMethodDefinition = genericClassInstance.GetType().GetMethod("SomeComplexMethod");
var genericMethodDefinition = openMethodDefinition.MakeGenericMethod(typeof(float));
return genericMethodDefinition.Invoke(genericClassInstance, new object[] { inputA, inputB });
}
static object InvokeComplexMethodDynamically(object genericClassInstance, string inputA, float inputB) {
return ((dynamic) genericClassInstance).SomeComplexMethod(inputA, inputB);
}
}
Code snippet taken from "Complete C# Quick Reference - C# 2, 3 and 4".