Here is my problem;
public class MyClass<T> { public void DoSomething(T obj) { .... } }
What I've done:
var classType = typeof(MyClass<>); Type[] classTypeArgs = { typeof(T) }; var genericClass = classType.MakeGenericType(classTypeArgs); var classInstance = Activator.CreateInstance(genericClass); var method = classType.GetMethod("DoSomething", new[]{typeof(T)}); method.Invoke(classInstance, new[]{"Hello"});
In the above case, the exception I get is: Limited-bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
If I try to create a generic method, it will end again with an exception: MakeGenericMethod can only be called for a method that has the MethodBase.IsGenericMethodDefinition property set.
How do I call a method?
source share