Late binding of type (class) name

How do I do late binding (or reflection) of a class name to instantiate it   ex:

Public Function GetData(ByVal p_Type as string) as data

Dim obj as new <libraryname>[p_Type]

return obj.getData

All the classes in the library will have a standard interface.

Thanx,

 




Answer this question

Late binding of type (class) name

  • Gaurav Srivastava

    The fact that nothing throws makes me think that asm.CreateInstance() can't find the Clients type. Is Clients really the fully namespace qualified type name Could you have forgotten to prepend the root namespace Is the type public

    I would set a breakpoint on the asm.CreateInstance line and check what asm.GetExportedTypes() returns.

    Best regards,
    Johan Stenberg



  • leandro Oliveira

    Is this what you are looking for:

    http://msdn2.microsoft.com/en-us/library/system.activator.createinstance(VS.80).aspx

    Best regards,
    Johan Stenberg



  • Vinny Davi

    Ok, that worked. Next question.

    What I am trying to do is capture the current method parameters, serialize them, send them through a web service, create instance of assembly on server and call the method with the same parameters. I am able to serialize the parameters list with the following:

    Dim parms() As System.Reflection.ParameterInfo
    Dim strm As New System.IO.MemoryStream
    Dim fmtr As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim outbyt() As Byte
    Dim mth As System.Reflection.MethodBase
    mth = System.Reflection.MethodBase.GetCurrentMethod
    outparms = mth.GetParameters()
    fmtr.Serialize(strm, parms)
    outbyt = strm.ToArray

    At this point, I can send the byte array to the server. How do I reconstruct the parameters to invoke the method on the server side

    Dim parms() As System.Reflection.ParameterInfo
    Dim fmtr As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    parms = CType(fmtr.Deserialize(new system.IO.MemoryStream(byt)), System.Reflection.ParameterInfo())
    obj = asm.CreateInstance(classname, True)

    This is as far as I have gotten.

    Thanx for the help.

    Robert



  • Guy_Dupre

    Close, but I'm missing something. Here is the code I have so far:

    Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.Load("DAL")
    Dim obj As Object
    Dim comp As DAL.DALBase

    obj = asm.CreateInstance("Clients", True)
    comp =
    CType(obj, DAL.DALBase)

    I don't get any errors, but comp is a null reference. What am I doing wrong

    Each class in my "DAL" assembly has a standard interface



  • Late binding of type (class) name