473,762 Members | 5,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strin g objectType, ref string suppMsg)
{
string LoadObj = string.Format(" WitsmlObjects.{ 0}",objectType) ;
witsObject = WitsmlObjectsDl l.CreateInstanc e(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(baseObje ct);)
{
.....
}
// this fail, but would be ok to
(Object)fac.Cre ateObj(strObj,r ef suppMsg). if cast objec ot baseobject
fail.
obj = (BaseObject)fac .CreateObj(strO bj,ref suppMsg);
Nov 15 '05 #1
1 3042
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*******@yaho o.com> wrote in message
news:42******** *************** ***@posting.goo gle.com...
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(strin g objectType, ref string suppMsg)
{
string LoadObj = string.Format(" WitsmlObjects.{ 0}",objectType) ;
witsObject = WitsmlObjectsDl l.CreateInstanc e(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(baseObje ct);)
{
.....
}
// this fail, but would be ok to
(Object)fac.Cre ateObj(strObj,r ef suppMsg). if cast objec ot baseobject
fail.
obj = (BaseObject)fac .CreateObj(strO bj,ref suppMsg);

Nov 15 '05 #2

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

Similar topics

0
3853
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 (listed below) even though I have StrongNamed my application and used the .NET Configuration Tool to create a Code Access Group in the Machine Policy that gives FullTrust to assemblies with the StrongName that I created. If you uncomment the code...
0
339
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 the only difference between the il BEFORE the line throwing the exception is a BR.S as opposed to BR, due to the length of the branch required, which makes sense. anyone know what is going on?
5
3434
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 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
4
7552
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 SP2, and IIS's SMTP and WWW servers are installed. Here is the error trace: System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an...
6
4056
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 instantiating a particular class in my project but the dialog doesn't tell me what code in the class is causing the problem (and it a large class). How can I get the IDE to take me to the offending line of code? If I put this line in my...
7
3628
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 get to item 250 i get a "system.invalid cast exception" and "specified cast is not valid". Once i get this error and try to go to the next record.. every record after gets the same error... It's like the com connection to outlook has been lost....
0
1603
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 http://ftp.mediaburst.com/ben/wsdl/Types.wsdl but i dont know why. I am trying to consume a SOAP::Lite perl web service so i think that requires that i use a "rpc" style binding and encoded body instead of
4
10871
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 this error from the compiler when trying to compile the solution. Does anyone know why this occurs, and how it can be resloved, i would be very grateful indeed.
11
1788
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 the object is (i.e.: is it still cast as a Derived class, or is it cast as a Base class?) Here is some pseudo code to show the example. Class Base .... End Class
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9378
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,...
1
9927
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
9812
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
8814
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
6640
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();...
0
5268
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3914
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
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.