I have a question. Is it possible to call generic method using reflection in .NET?
I tried the following code
var service = new ServiceClass();
Type serviceType = service.GetType();
MethodInfo method = serviceType.GetMethod("Method1", new Type[]{});
method.MakeGenericMethod(typeof(SomeClass));
var result = method.Invoke(service, null);
But it throws the following exception “Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.”
You’re not using the result of
MakeGenericMethod– which doesn’t change the method you call it on; it returns another object representing the constructed method. You should have something like:(or use a different variable, of course).