0

I tried to get data from database by using GetType.

Example in VB.net:

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.

1 Answers1

0

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?

Community
  • 1
  • 1
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • I'm presuming you mean `method` is null. Check that the GetAll method exists on UnitOfWork and that it's a public instance method. Without more knowledge of what GetAll is I can't help further – FloatingKiwi Sep 06 '16 at 07:31
  • Its work! Thank so much @FloatingKiwi. GetAll will return IQueryable. I just use this function `Type.GetType("MyNamespace.Student, MyNamespace")` Do you know how to call where function? Actually what i want to do `Me.unitofwork.GetAll(Of MyNamespace.Student).where(function(x) x.Id = 1)` – user2156181 Sep 06 '16 at 07:47
  • Me.unitofwork.GetAll(Of MyNamespace.Student).where(function(x) x.Id = 1).FirstorDefault – user2156181 Sep 06 '16 at 08:02