Home All Groups Group Topic Archive Search About

DirectCast of object to interface doesn't work

Author
14 Jul 2006 8:06 AM
lothar.behrens@lollisoft.de
Hi,

I am trying to load an assembly at runtime (plugin). Having the object
(as Object), I like to
cast it to the interface. The interface is defined as follows:

Public Interface IVoimDescriptor
    Function getVendorName() As String
    Function getVoimDescription() As String
'...
End Interface

The class, I get, is defined as follows:

Public Class VoimDescriptor
    Implements IVoimDescriptor

    Public Function getVendorName(ByVal i As Integer) As String
Implements IVoimDescriptor.getVendorName

    End Function

    Public Function getVoimDescription() As String Implements
IVoimDescriptor.getVoimDescription

    End Function
End Class


Trying to cast the object to IVoimDescriptor fails with
InvalidCastException.

Why, and how could I do this ?

The Plugin implements classes based on interfaces and only the
interfaces are public.

Thanks, Lothar

            assem = assem.LoadFrom(cbPlugins.SelectedItem)
            Dim o As Object =
assem.CreateInstance("SampleVoim.VoimDescriptor")


            Dim descriptor As IVoimDescriptor = DirectCast(o,
IVoimDescriptor) ' <<<<<<<<<

            txtDesc.Text = descriptor.getVoimDescription()

Author
14 Jul 2006 9:33 AM
lothar.behrens@lollisoft.de
Hi,

I have solved it :-)

Creating an assembly with only that interfaces and referencing to it
helped.

Regards, Lothar

lothar.behr***@lollisoft.de wrote:
Show quoteHide quote
> Hi,
>
> I am trying to load an assembly at runtime (plugin). Having the object
> (as Object), I like to
> cast it to the interface. The interface is defined as follows:
>
> Public Interface IVoimDescriptor
>     Function getVendorName() As String
>     Function getVoimDescription() As String
> '...
> End Interface
>
> The class, I get, is defined as follows:
>
> Public Class VoimDescriptor
>     Implements IVoimDescriptor
>
>     Public Function getVendorName(ByVal i As Integer) As String
> Implements IVoimDescriptor.getVendorName
>
>     End Function
>
>     Public Function getVoimDescription() As String Implements
> IVoimDescriptor.getVoimDescription
>
>     End Function
> End Class
>
>
> Trying to cast the object to IVoimDescriptor fails with
> InvalidCastException.
>
> Why, and how could I do this ?
>
> The Plugin implements classes based on interfaces and only the
> interfaces are public.
>
> Thanks, Lothar
>
>             assem = assem.LoadFrom(cbPlugins.SelectedItem)
>             Dim o As Object =
> assem.CreateInstance("SampleVoim.VoimDescriptor")
>
>
>             Dim descriptor As IVoimDescriptor = DirectCast(o,
> IVoimDescriptor) ' <<<<<<<<<
>
>             txtDesc.Text = descriptor.getVoimDescription()
Author
14 Jul 2006 11:05 AM
Herfried K. Wagner [MVP]
<lothar.behr***@lollisoft.de> schrieb:
> I am trying to load an assembly at runtime (plugin). Having the object
> (as Object), I like to cast it to the interface.
>[...]
> Trying to cast the object to IVoimDescriptor fails with
> InvalidCastException.

Plug-ins and cast exceptions
<URL:http://www.yoda.arachsys.com/csharp/plugin.html>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>