473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(msLib Path +
"MyApp.dll").Ge tModules(False) (0)
oType = oMod.GetType("M yCompany.MyApp. CMyClass", False)
oLocalObject = Activator.Creat eInstance(oType )

Try
'This is Just added to prove the point that it should
be able to cast this.
If oLocalObject.Ge tType.GetInterf aces(0).FullNam e =
"MyCompany.MyAp p.IMyInterface" Then _
oReturnValue = CType(oLocalObj ect,
MyCompany.MyApp .IMyInterface)

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

Return oReturnValue

TIA,
Andy
Jul 21 '05 #1
4 1568

"Andy Franks" <ca********@jun o.com> wrote in message
news:5f******** *************** ***@posting.goo gle.com...
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********@jun o.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.co m>
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
23252
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 (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
35
2712
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
2637
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 says this about the <ctype.h> functions: "The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the
7
3683
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; but it complains for the following lines
3
1674
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"); // This line of code compares the objects, but the compiler doesn't like my casting. return ((type)object1 <= (type)object2) ? true : false; The compiler doesn't like my casting of (type)object1 or (type)object2.
8
9480
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 System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
7
13717
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
4635
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 exists (are you missing a cast?) Any help with this would be appreciated...
32
2398
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" variable, which of the following lines are correct:
4
3045
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> class ListLogic { public: struct LogicBase {
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.