Reflecting a type and casting causes error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    #1

    Reflecting a type and casting causes error

    Hi all,

    The following code is meant to be used to load a class from an assembly
    and return a property from it. The class being loaded will always
    inherit from the base class of MyLicenceBase. However, I just cannot
    get past the CType below when trying to convert the loaded class to the
    proper base type. Is it possible to do this? I am doing this cast
    elsewhere in my code (but not using reflection) so I can't see why it
    won't work here. I'd rather not do an invoke using strings to define
    the parameters etc.

    Any help appreciated very much...

    Function CheckClass(ByVa l AssemblyPath As String, ByVal
    AssemblyType As String) As String
    Dim AssemblyToLoad As Assembly
    Dim BaseObject As Licencing.MyLic enceBase
    Dim TempObject As Object

    AssemblyToLoad = Assembly.LoadFr om(AssemblyPath )

    ' The following should fire the default constructor for the
    class.
    TempObject = AssemblyToLoad. CreateInstance( AssemblyType)

    ' The class we instantiated inherits from a base type, cast
    to that so we can programmaticall y access properties.
    ' The following line generates an InvalidCastExce ption!
    BaseObject = CType(TempObjec t, Licencing.MyLic enceBase)

    ' Return a test property from the class.
    Return BaseObject.Titl e

    End Function

    Thanks

    Ryan

Working...