Casting error in late-binding

A

Andy Franks

Hi All,

This is driving me nuts, especially since I have this working for
my main application with no problem. I suspect it might be due to
Namespace conflicts, but I'm not positive. Any help is vastly
appreciated.

This is part of a routine for switching between using local and remote
business logic libraries. Essentially, it's a fairly simple
late-binding routine. However, When I try to cast oLocalObject to
the IMyInterface, I get an Invalid Casting exception. However, If I
use early-binding (not an option, unfortunatly) this casting works.
The casting also works in the Remoting option (not included here).

Here's how the objects look:
(Yes, both assemblies share the same namespace. This is needed to
have the interface work for remoting)

MyApp.exe
MyCompany.MyApp.IMyInterface

MyApp.dll
MyCompany.MyApp.IMyInterface
MyCompany.MyApp.CMyClass (Implements MyCompany.MyApp.IMyInterface)


Code:
Dim oType As Type, oMod As [Module]
Dim oLocalObject As Object
Dim oReturnValue As MyCompany.MyApp.IMyInterface

oMod = [Assembly].LoadFile(msLibPath +
"MyApp.dll").GetModules(False)(0)
oType = oMod.GetType("MyCompany.MyApp.CMyClass", False)
oLocalObject = Activator.CreateInstance(oType)

Try
'This is Just added to prove the point that it should
be able to cast this.
If oLocalObject.GetType.GetInterfaces(0).FullName =
"MyCompany.MyApp.IMyInterface" Then _
oReturnValue = CType(oLocalObject,
MyCompany.MyApp.IMyInterface)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Return oReturnValue



TIA,
Andy
 
D

David Browne

Andy Franks said:
Hi All,

This is driving me nuts, especially since I have this working for
my main application with no problem. I suspect it might be due to
Namespace conflicts, but I'm not positive. Any help is vastly
appreciated.

This is part of a routine for switching between using local and remote
business logic libraries. Essentially, it's a fairly simple
late-binding routine. However, When I try to cast oLocalObject to
the IMyInterface, I get an Invalid Casting exception. However, If I
use early-binding (not an option, unfortunatly) this casting works.
The casting also works in the Remoting option (not included here).

Here's how the objects look:
(Yes, both assemblies share the same namespace. This is needed to
have the interface work for remoting)

MyApp.exe
MyCompany.MyApp.IMyInterface

MyApp.dll
MyCompany.MyApp.IMyInterface
MyCompany.MyApp.CMyClass (Implements MyCompany.MyApp.IMyInterface)

Why is the interface declared in both assemblies? It should be declared in
one place only. You may need a third assembly: MyAppInterfaces.dll to hold
the interface definitions and be referenced from each of the other
assemblies.

David
 
M

Mattias Sjögren

Andy,
MyApp.exe
MyCompany.MyApp.IMyInterface

MyApp.dll
MyCompany.MyApp.IMyInterface

So you have two definitions of the IMyInterface? That would explain
why it doesn't work. You have to use the same type.



Mattias
 
C

caughtfire

Thanks for all the suggestions. From my reading, what I'm trying to do
isn't possible.

Thanks anyways,

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top