user2156181 user2156181 - 1 year ago 90 Question

get Generic Class from GetType

I tried to get data from database by using GetType.

Example in

Dim genericObject = Me.UnitOfWork.GetAll(Of GetType("MyNamespace.Student"))()

For sure this code got error. But i don't have any idea to retrieve data from database by using string of class name.

My project is, i tried to create dynamic document merging.

Answer Source

You need to do Me.UnitOfWork.GetAll(of MyNamespace.Student)(). If the class isn't known at compile time then you'll need to invoke it via reflection

Dim method As MethodInfo = Me.UnitOfWork.GetType().GetMethod("GetAll")
Dim generic As MethodInfo = method.MakeGenericMethod(GetType("MyNamespace.Student"))
generic.Invoke(Me.UnitOfWork, Nothing)

See this answer for more details: How do I use reflection to call a generic method?