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

TargetInvocationException

Hello,

I'm receiving a "TargetInvocationException" on the follow line of
code:

datagridServers.Invoke(new
UpdateDisplayCallback(this.UpdateServerDisplay), new object[]
{ server });

The method:

private void AddServer(MyServerClass server){
if(server != null)
arraylistServers.Add(server);

if(datagridServers.InvokeRequired)
datagridServers.Invoke(new
UpdateDisplayCallback(this.UpdateServerDisplay), new object[]
{ server });
else
this.UpdateServerDisplay(server);
}

The signature for UpdateServerDisplay is "private void
UpdateServerDisplay(MyServerClass server)"
The signature for UpdateDisplayCallback is "private delegate void
UpdateDisplayCallback(MyServerClass server)"

In one of many attempts to solve the problem, I placed a breakpoint on
the line ("if(server != null)") and the 'server' instance contains
everything that it should contain, meaning that it is an instance of
MyServerClass. But when I compile and run the application, I get a
TargetInvocationException on the line I pointed out above, and the
InnerException says "Unable to cast object of type
'App.ServerInfoClass' to type 'App.MyServerClass'. It seems that it's
trying to cast a ServerInfoClass into a MyServerClass, for no reason?

I can't seem to find where I'm going wrong here. Stepping through the
program, I'm passing the correct type to the method, but it seems to
have a problem with the "datagridServers.Invoke...") line. I've been
trying to figure this out for a few days, at no avail ofcourse. Any
hints would be greatly appreciated. Thanks
Dec 13 '07 #1
1 3685
On Wed, 12 Dec 2007 20:10:20 -0800, tcomer <tc****@gmail.comwrote:
[...]
In one of many attempts to solve the problem, I placed a breakpoint on
the line ("if(server != null)") and the 'server' instance contains
everything that it should contain, meaning that it is an instance of
MyServerClass.
The exception you describe (the inner one, that is) is specifically
telling you that it's _not_ an instance of MyServerClass, at least not of
the MyServerClass the method and delegate in question is expecting.
But when I compile and run the application, I get a
TargetInvocationException on the line I pointed out above, and the
InnerException says "Unable to cast object of type
'App.ServerInfoClass' to type 'App.MyServerClass'. It seems that it's
trying to cast a ServerInfoClass into a MyServerClass, for no reason?
My experience has been that the .NET runtime doesn't cast things "for no
reason".

In this particular case, _some_ cast has to occur, because the code
receiving the object only gets it as an instance of an "object". So
apparently, in spite of the instance looking a lot like what you know of
as a MyServerClass, it in fact is something else.

The cast doesn't happen "for no reason", and it fails because the object
isn't a type compatible with the desired type.
I can't seem to find where I'm going wrong here. Stepping through the
program, I'm passing the correct type to the method, but it seems to
have a problem with the "datagridServers.Invoke...") line. I've been
trying to figure this out for a few days, at no avail ofcourse. Any
hints would be greatly appreciated. Thanks
You'll probably need to post a concise-but-complete example of code that
reliably reproduces the problem.

The code you posted doesn't include any of the parts that would actually
be relevant. Looking at the line that generates the error is not enough
when the error is related to how the objects in question are created and
how their types are defined.

Obviously there's some sort of type incompatibility going on, and if I had
to guess I'd suspect types coming from two different assemblies. But
that's just a guess and there's no way to actually know what's going on
without more information.

Pete
Dec 13 '07 #2

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

Similar topics

4
by: dudi | last post by:
Hi, very puzzling... I have one perfectly working solution, in which I use a a.dll which has a class A. that class loads data from the database and for each row deserialize the cell into class A1....
1
by: Greg Patrick | last post by:
My problem: I load an some assemblies (strong named) from a byte array using Assembly.Load(byte). They load fine. But one one of them is actually accessed, it's referenced assemblies can't be...
2
by: 2G | last post by:
Hi, I'm having some problems creating an instance of the dataset I compile in memory and I don't know why. I what to do this to Invoke WriteXmlSchema or GetSchemaSerializable so I could save...
0
by: Gemma M | last post by:
Hi, I have a C# program (code fragment below), which runs forever, processing incoming jobs. Part of this program launches a COM object. However, after a while I get : ...
0
by: chaf2701 | last post by:
Hi, I was in trouble with the System.Windows.Forms.ImageList control from VC.NET. I was doing the following : 1. New Project, C#, Windows Application 2. Add ImageList component imageList1 to...
1
by: JP | last post by:
Hi, I am facing a strange problem, please take a look at the code below: public SaveTree() { ........ ...... ......//some code Initranges()
0
by: Sutabi | last post by:
I'm trying to compile a "Release" build of a project, and in debug more it compiles and runs fine. In release mode it builds, but does not run and returns this exception. I have no idea what this...
3
by: Lambuz | last post by:
Hi all, when I use MethosInfo.Invoke method I obtain a TargetInvocationException where inner exception is +{"classBase.OpenConn cannot be invoked directly" } System.Exception. I'm using the...
2
by: Zytan | last post by:
I just got a TargetInvocationException from a WebBrowser control, after calling WebBrowser.Document.Write(text), which (internally) calls System.Windows.Forms.HtmlDocument.Write(String text),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.