By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

Casting error in late-binding

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Andy Franks" <ca********@juno.com> wrote in message
news:5f**************************@posting.google.c om...
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
Jul 21 '05 #2

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #3

P: n/a
Andy Franks <ca********@juno.com> wrote:
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.


See http://www.pobox.com/~skeet/csharp/plugin.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

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

Thanks anyways,

Andy

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.