Francis
Francis

Reputation: 21

Using reflection I need to invoke a method with (of T) parameter

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

Upvotes: 2

Views: 1156

Answers (1)

Tom
Tom

Reputation: 3374

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)

Upvotes: 3

Related Questions