2

I'm trying to make this code with reflection since I want it to manage Technician and other types too.

m_Technician = m_Entities.CreateObject(Of Technician)()     'line#1
m_Technician.IDTechnician = Guid.NewGuid()
m_Entities.AddObject("Technicians", m_Technician)

I used this code with reflection to fill the entity and it work perfectly.

m_Entity = GetType(RFOPSEntities). _
           GetMethod(FillMethodName).Invoke(m_Entities, New Object() {uniqueKey})

So I tried something like that for the line #1 :

m_Entity = GetType(RFOPSEntities). _
           GetMethod("CreateObject"). _
           Invoke(m_Entities, New Object({GetType("Technician")})

I think my difficulty is to pass the (Of Technician)

Thank you

manji
  • 47,442
  • 5
  • 96
  • 103
Francis
  • 21
  • 2
  • possible duplicate of [How to use reflection to call generic Method?](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – usr May 30 '14 at 17:10

1 Answers1

3

You can use the MakeGenericMethod function to produce a generic MethodInfo from which you can invoke.

m_Entity = GetType(RFOPSEntities). _
           GetMethod("CreateObject").MakeGenericMethod(GetType(Technician)). _
           Invoke(m_Entities)
Tom
  • 3,354
  • 1
  • 22
  • 25
  • Thank you Tom, It work great. I forgot it was a generic method, that make sense. – Francis May 05 '11 at 13:58
  • Now I'm trying to figure out how to do the second line wich is to set the property: m_Technician.IDTechnician = Guid.NewGuid() it could be m_entity.IDProduct = Guid.NewGuid(), I'm tryingto figure out myselft, but sometimes help don't hurt. Thank you – Francis May 05 '11 at 14:01
  • @Francis glad it worked! Assuming that m_Technician is an object and you're wanting to do this totally by reflection, then it'd have to be along the lines of m_Entity.GetType().GetProperty("IDTechnician").SetValue(m_Entity, Guid.NewGuid(), Nothing) – Tom May 05 '11 at 14:56