473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Casting error in late-binding

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
4 1537

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
3
by: Steve Teeples | last post by:
Can someone explain how to cast an object to a specific type during runtime? // This line of code tells me the objects type. System.Type type = System.Type.GetType("string of the type"); //...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
4
by: sck10 | last post by:
Hello, I am converting some code from VB to C# and ran into a conversion error: Cannot implicitly convert type 'object' to 'System.Web.UI.WebControls.RadioButtonList'. An explicit conversion...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.