I’m trying to call a generic method I’ve created called LoadItems<T>(). This method does a bunch of operations on a List<T> of items it returns from the database.
The problem I have is in calling the LoadItems<T>() method. All I have to work with is an object. I want to resolve this object into a T so that I can call my method.
In a pseudo way of explaining:
object theObject = GetTheObject();
LoadItems<GetGenericType(theObject)>();
Is there any way to do this?
Thanks a ton
You’ll have to use reflection or change your design.
If you want to use reflection, the process is complicated:
Note that
resultswill be anobject. If you want it to be a specificList<>type, you’re out of luck. You don’t know what the type is at compile time. You can cast it to the non-genericIList, or use some LINQ expression to convert it to something more useful, like this:As always, read the documentation on the functions I listed above if you aren’t sure what’s going on.