Tutorial :Can't invoke dynamic method from generic class



Question:

I'm trying to work with two contexts in a generic repository and I should invoke the static method GetObjectContext() with dynamic type like ObjectContextManager<DynamicType>.GetObjectContext().

private DataContext GetDataContext()      {          Type type = GetContainerType();          Type paoloGenericClassType = typeof(ObjectContextManager<>).MakeGenericType(type);            MethodInfo method = paoloGenericClassType.GetMethod("GetObjectContext", BindingFlags.Static);          return method.Invoke(null, BindingFlags.Static, null, null, null) as DataContext;      }  

I'm try different variants, but it doesn't work. How can I do this?


Solution:1

I suspect the problem is just with your binding flags. Try BindingFlags.Static | BindingFlags.Public, assuming it's a public method.

If that doesn't work, please tell us what actually happens, rather than just saying it doesn't work.


Solution:2

Checkout this article on how to invoke generic methods via reflection.

http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/10/invoking-generic-methods-with-reflection.aspx

http://avantprime.com/articles/view-article/16/invoke-a-generic-method-with-parameters-using-reflection

DaTribe


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »