473,326 Members | 2,061 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,326 software developers and data experts.

exception when cast the reflection created object.

hi, I get the problem here, I have exception thrown when I try to cast
the reflection created object.
first there is BaseObject dll that implemation a interface, factory
dll use the reflection load the dll, create the object and return.
BaseObject reference the factory dll.

factory.dll
public object CreateObj(string objectType, ref string suppMsg)
{
string LoadObj = string.Format("WitsmlObjects.{0}",objectType);
witsObject = WitsmlObjectsDll.CreateInstance(LoadObj);
}

test code:
Factory fac = new Factory();
object s = fac.CreateObj("baseObject",ref suppMsg);
// if check the type both are not equal.

if(s.GetType()== typeof(baseObject);)
{
.....
}
// this fail, but would be ok to
(Object)fac.CreateObj(strObj,ref suppMsg). if cast objec ot baseobject
fail.
obj = (BaseObject)fac.CreateObj(strObj,ref suppMsg);
Nov 15 '05 #1
1 3022
Mark,

There are two usual causes of this:

1) The interface that's used on both sides must be the same interface in the
same assembly (ie the add-in must reference the interface in the main
assembly). If not, the interface will have the same name, but since it's
from a different assembly, it's the same type.

2) The interfaces must match on version. If you recompile the base assembly
but not the add-in, they end up with different version numbers and are
therefore not the same type.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"mark" <ma*******@yahoo.com> wrote in message
news:42**************************@posting.google.c om...
hi, I get the problem here, I have exception thrown when I try to cast
the reflection created object.
first there is BaseObject dll that implemation a interface, factory
dll use the reflection load the dll, create the object and return.
BaseObject reference the factory dll.

factory.dll
public object CreateObj(string objectType, ref string suppMsg)
{
string LoadObj = string.Format("WitsmlObjects.{0}",objectType);
witsObject = WitsmlObjectsDll.CreateInstance(LoadObj);
}

test code:
Factory fac = new Factory();
object s = fac.CreateObj("baseObject",ref suppMsg);
// if check the type both are not equal.

if(s.GetType()== typeof(baseObject);)
{
.....
}
// this fail, but would be ok to
(Object)fac.CreateObj(strObj,ref suppMsg). if cast objec ot baseobject
fail.
obj = (BaseObject)fac.CreateObj(strObj,ref suppMsg);

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ant Corrie | last post by:
I am at a loss as to why this is not working. I am trying to setup a TextWriterTraceListener in my application config file and then execute the app from the network. I get a security exception...
0
by: guy | last post by:
i have a method which throws an exception. the thing i dont understand is that the only change between the two versions is a line of code AFTER the one that throws the exception - see code below...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
4
by: Aren Cambre | last post by:
Why does SmtpMail.Send throw an exception if the MailMessage's BodyFormat = MailFormat.Html? I've searched all over the place and cannot find a solution anywhere. I am running this on Windows XP...
6
by: Steve Long | last post by:
Help, I'm running VS.NET 2003 and when I try to start my application, I get the "unhandled exception" dialog instead of the IDE highlighting the offending line of code. The problem appears to be...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
0
by: Ben Crinion | last post by:
Hi Im getting the error below when i try and cosume this web service http://ftp.mediaburst.com/ben/wsdl/SearchService.wsdl. I think the error is due to the array in...
4
by: ffhansix | last post by:
Hi, I am trying to reference a COM component (.dll) from Visual Studio 2005 in my windows application in C# by adding a reference to a third party.dll file (tried .type library also), and i get...
11
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of...
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...
1
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.