473,511 Members | 15,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

An InvalidCastException

hi ,

The Code like this:

public static Mapping Deserialize(string strXmlFilePath)
{
XmlSerializer s = new XmlSerializer(typeof(Mapping));
using (StreamReader sr = new StreamReader(strXmlFilePath))
{
object o = s.Deserialize(sr);
Mapping m = (Mapping)o;
return m;
}
}

Sometimes ,it will throw an InvalidCastException.
The strXmlFilePath refer to a same file ,but sometimes it works well
,sometimes it throws a exception.
I can't catch this exception in a debuger,because it always works well in
the debuger.
Anyone know what's wrong?

thanks
Jul 25 '05 #1
5 1305
Jan
if the Deserialize function fails it will return null and when you try
to cast that to Mapping you'll get an invalid cast exception. It's
probably working in Debug mode because your using a relative path.

Jan

Jul 25 '05 #2
Jan <ja***********@gmail.com> wrote:
if the Deserialize function fails it will return null and when you try
to cast that to Mapping you'll get an invalid cast exception. It's
probably working in Debug mode because your using a relative path.


No, casting null works fine:

using System;

class Test
{
static void Main()
{
object o = null;
string s = (string)o;

Console.WriteLine ("s is null? {0}", s==null);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 25 '05 #3
Jan
Ah yes, my mistake.

It must be the Deserialize function failing to parse you xml stream
into your mapping.

Jul 26 '05 #4
Was the object serialized in the same program or from a different
program? If from a different program, did you copy the Mapping class
file to the other program and compile or did you put it in a
ClassLibrary and compile it that way?

You need to put your mapping class in a ClassLibrary if you intend to
serialize/deserialize across applications.

Jul 26 '05 #5
I'm using the Mapping class in two program , one is an add-in for VS IDE, the
other is a form application.The exception just sometimes occurs in the
add-in. Yes, the Mapping class is put in an individual class library, the two
program reference to the same dll. And I can promise these two program all
open the same and valid file.

And I don't think in XML serialization assembly version is a problem ,
assembly version just may cause problem in binary serialization. In XML
serialization, where the class resides in or even whether the class is
complete identity is not important.

"Chris Dunaway" wrote:
Was the object serialized in the same program or from a different
program? If from a different program, did you copy the Mapping class
file to the other program and compile or did you put it in a
ClassLibrary and compile it that way?

You need to put your mapping class in a ClassLibrary if you intend to
serialize/deserialize across applications.

Jul 27 '05 #6

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

Similar topics

0
1383
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
1
3669
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
11
3095
by: Roy Lawson | last post by:
I have no idea what is going on here. I wrote a simple application in VB.NET to generate a Crystal Report, and I am now trying to move it to ASP.NET with Crstal Enterprise. I wish I could tell...
4
2647
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
623
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
1
18135
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
8
4248
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
8
5733
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i trying to collect windows services. i'm getting inconsistencies. the collection works every time locally. but remote collection yields different result depending on my lab environment. 1) in...
4
1833
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
8
4940
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String...
0
7137
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
7417
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...
1
7074
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...
0
7506
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...
1
5063
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...
0
3219
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...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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 ...
0
445
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...

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.