Reflection: call method with a shared list as a result

I have the following example class:

public class MyClass<T> { public IList<T> GetAll() { return null; // of course, something more meaningfull happens here... } } 

And I would like to call GetAll with reflection:

 Type myClassType = typeof(MyClass<>); Type[] typeArgs = { typeof(object) }; Type constructed = myClassType.MakeGenericType(typeArgs); var myClassInstance = Activator.CreateInstance(constructed); MethodInfo getAllMethod = myClassType.GetMethod("GetAll", new Type[] {}); object magicValue = getAllMethod.Invoke(myClassInstance, null); 

As a result (in the last line above the code):

Limited bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.

Ok, try again:

 MethodInfo getAllMethod = myClassType.GetMethod("GetAll", new Type[] {}); getAllMethod = getAllMethod.MakeGenericMethod(typeof(object)); object magicValue = getAllMethod.Invoke(myClassInstance, null); 

As a result (on the second last line above the code):

System.Collections.Generic.IList`1 [T] GetAll () is not a GenericMethodDefinition. MakeGenericMethod can only be called for a method that has the MethodBase.IsGenericMethodDefinition property set.

What am I doing wrong here?

+4
source share
4 answers

I tried this and it works:

 // Create generic type Type myClassType = typeof(MyClass<>); Type[] typeArgs = { typeof(object) }; Type constructed = myClassType.MakeGenericType(typeArgs); // Create instance of generic type var myClassInstance = Activator.CreateInstance(constructed); // Find GetAll() method and invoke MethodInfo getAllMethod = constructed.GetMethod("GetAll"); object result = getAllMethod.Invoke(myClassInstance, null); 
+6
source

I noticed (not sure if this is just a mistake in your example) that there is a problem with your code. myClassInstance will be of type object , so you cannot call it GetMethod(...) . I think you can call it a type. Secondly, you pass baseRepo as an object to call the method - do you want to call the method when creating the type - in this case, the variable myClassInstance ?

If you change your code this way, you should have something like the code below (which works when testing):

 Type classType = typeof(MyClass<>); Type[] typeArgs = { typeof(object) }; Type fullClassType = classType.MakeGenericType(typeArgs); var classInstance = Activator.CreateInstance(fullClassType); MethodInfo method = fullClassType.GetMethod("GetAll", new Type[0]); object result = method.Invoke(classInstance, null); 
+1
source

It works:

  public static void TestMethod() { Type myClassType = typeof(MyClass<>); Type[] typeArgs = { typeof(object) }; Type constructed = myClassType.MakeGenericType(typeArgs); var myClassInstance = Activator.CreateInstance(constructed); MethodInfo getAllMethod = constructed.GetMethod("GetAll", new Type[] { }); object magicValue = getAllMethod.Invoke(myClassInstance, null); } 

There are some errors in your code, for example:

  • You need to call GetMethod(...) on an object of type your general class (not on the instance).
  • getAllMethod.Invoke(...) requires an instance of the generic class that you created using Activator .
+1
source

If so, how do you use it, why are you trying to create a MyClass generic? It will be much faster:

 public class MyClass { public IList GetAll() { return null; // of course, something more meaningfull happens here... } } 

and then just call

 var myObject = new MyClass(); var myList = myObject.GetAll(); 
0
source

Source: https://habr.com/ru/post/1393383/


All Articles