473,507 Members | 2,374 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 1304
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
3668
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
4247
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
5732
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
4937
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
7223
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,...
0
7111
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
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
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
7031
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
4702
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...
0
3191
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...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
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.