473,388 Members | 1,375 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,388 software developers and data experts.

An InvalidCastException again

hi ,

I had asked this question serveral days ago, but it's still unresolved. And
I have found some new things. this error just occured sometimes,not always.

the code like this:(It's running in a VS IED Add-In.)

public static Mapping Deserialize(string strXmlFilePath)
{
XmlSerializer s = new XmlSerializer(typeof(Mapping));
using (StreamReader sr = new StreamReader(strXmlFilePath))
{
object o = s.Deserialize(sr);
Type t = o.GetType();
Type t2 = typeof(Mapping);
Mapping m = (Mapping)o;
return m;
}
}
--------------------when the exception is
catched------------------------------------------
Now, I catch the exception in the debuger: variable o references to a
instance of Mapping class, but the sentence Mapping m = (Mapping)o throw a
InvalidCastException.

and:
t.Assembly.CodeBase
"file:///c:/documents and settings/whf.ztlq/application
data/microsoft/visualstudio/7.1/projectassemblies/51an1oxl01/persistlayer.dll"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
false
-------------------------------------------------------------------------------------------

---------------when it run
correctly-----------------------------------------------------
t.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
true

--------------------------------------------------------------------------------------------

Any body can tell me why?
thanks

WangHF
Jul 29 '05 #1
3 2609
Sure. you have two classes called "Mapping". One created by you, the other
created by the system, probably when you referenced a web service or an XML
Schema.

Fully qualify the mapping class that you want on the line:
XmlSerializer s = new XmlSerializer(typeof(Mapping));

so this becomes
XmlSerializer s = new
XmlSerializer(typeof(MyNameSpace.MyPersistLayer.Ma pping));

(I don't know the names of your namespaces).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"WangHF" <Wa****@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
hi ,

I had asked this question serveral days ago, but it's still unresolved.
And
I have found some new things. this error just occured sometimes,not
always.

the code like this:(It's running in a VS IED Add-In.)

public static Mapping Deserialize(string strXmlFilePath)
{
XmlSerializer s = new XmlSerializer(typeof(Mapping));
using (StreamReader sr = new StreamReader(strXmlFilePath))
{
object o = s.Deserialize(sr);
Type t = o.GetType();
Type t2 = typeof(Mapping);
Mapping m = (Mapping)o;
return m;
}
}
--------------------when the exception is
catched------------------------------------------
Now, I catch the exception in the debuger: variable o references to a
instance of Mapping class, but the sentence Mapping m = (Mapping)o throw a
InvalidCastException.

and:
t.Assembly.CodeBase
"file:///c:/documents and settings/whf.ztlq/application
data/microsoft/visualstudio/7.1/projectassemblies/51an1oxl01/persistlayer.dll"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
false
-------------------------------------------------------------------------------------------

---------------when it run
correctly-----------------------------------------------------
t.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
true

--------------------------------------------------------------------------------------------

Any body can tell me why?
thanks

WangHF

Jul 29 '05 #2
Thank you very much.
But, the MAIN problem is that this error just occured SOMETIMES, NOT ALWAYS.
And, in a windows form application, I'm using the same Mapping class in the
same persistlayer.dll, this error never occured in this form application. So
I think this error must be related with Visual Studio and it's mechanism of
loading add-in.
How do you think about this?

"Nick Malik [Microsoft]" wrote:
Sure. you have two classes called "Mapping". One created by you, the other
created by the system, probably when you referenced a web service or an XML
Schema.

Fully qualify the mapping class that you want on the line:
XmlSerializer s = new XmlSerializer(typeof(Mapping));

so this becomes
XmlSerializer s = new
XmlSerializer(typeof(MyNameSpace.MyPersistLayer.Ma pping));

(I don't know the names of your namespaces).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"WangHF" <Wa****@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
hi ,

I had asked this question serveral days ago, but it's still unresolved.
And
I have found some new things. this error just occured sometimes,not
always.

the code like this:(It's running in a VS IED Add-In.)

public static Mapping Deserialize(string strXmlFilePath)
{
XmlSerializer s = new XmlSerializer(typeof(Mapping));
using (StreamReader sr = new StreamReader(strXmlFilePath))
{
object o = s.Deserialize(sr);
Type t = o.GetType();
Type t2 = typeof(Mapping);
Mapping m = (Mapping)o;
return m;
}
}
--------------------when the exception is
catched------------------------------------------
Now, I catch the exception in the debuger: variable o references to a
instance of Mapping class, but the sentence Mapping m = (Mapping)o throw a
InvalidCastException.

and:
t.Assembly.CodeBase
"file:///c:/documents and settings/whf.ztlq/application
data/microsoft/visualstudio/7.1/projectassemblies/51an1oxl01/persistlayer.dll"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
false
-------------------------------------------------------------------------------------------

---------------when it run
correctly-----------------------------------------------------
t.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t2.Assembly.CodeBase
"file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"

t ==t2
true

--------------------------------------------------------------------------------------------

Any body can tell me why?
thanks

WangHF


Jul 30 '05 #3
Sorry, Wang. I'm not going to bite. I gave you the answer. If you don't
want to use it, fine. It's not my defect.

By the way: when .net persists XML, it will often write a namespace into the
XML that matches the values that it used. Take a look to see if you don't
have XML tags that direct the deserializer to misinterpret the data.

And fix your code.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"WangHF" <Wa****@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...
Thank you very much.
But, the MAIN problem is that this error just occured SOMETIMES, NOT
ALWAYS.
And, in a windows form application, I'm using the same Mapping class in
the
same persistlayer.dll, this error never occured in this form application.
So
I think this error must be related with Visual Studio and it's mechanism
of
loading add-in.
How do you think about this?

"Nick Malik [Microsoft]" wrote:
Sure. you have two classes called "Mapping". One created by you, the
other
created by the system, probably when you referenced a web service or an
XML
Schema.

Fully qualify the mapping class that you want on the line:
XmlSerializer s = new XmlSerializer(typeof(Mapping));

so this becomes
XmlSerializer s = new
XmlSerializer(typeof(MyNameSpace.MyPersistLayer.Ma pping));

(I don't know the names of your namespaces).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"WangHF" <Wa****@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
> hi ,
>
> I had asked this question serveral days ago, but it's still
> unresolved.
> And
> I have found some new things. this error just occured sometimes,not
> always.
>
> the code like this:(It's running in a VS IED Add-In.)
>
> public static Mapping Deserialize(string strXmlFilePath)
> {
> XmlSerializer s = new XmlSerializer(typeof(Mapping));
> using (StreamReader sr = new StreamReader(strXmlFilePath))
> {
> object o = s.Deserialize(sr);
> Type t = o.GetType();
> Type t2 = typeof(Mapping);
> Mapping m = (Mapping)o;
> return m;
> }
> }
> --------------------when the exception is
> catched------------------------------------------
> Now, I catch the exception in the debuger: variable o references to a
> instance of Mapping class, but the sentence Mapping m = (Mapping)o
> throw a
> InvalidCastException.
>
> and:
> t.Assembly.CodeBase
> "file:///c:/documents and settings/whf.ztlq/application
> data/microsoft/visualstudio/7.1/projectassemblies/51an1oxl01/persistlayer.dll"
>
> t2.Assembly.CodeBase
> "file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"
>
> t ==t2
> false
> -------------------------------------------------------------------------------------------
>
>
>
> ---------------when it run
> correctly-----------------------------------------------------
> t.Assembly.CodeBase
> "file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"
>
> t2.Assembly.CodeBase
> "file:///D:/MyPersistLayer/ORMappingGenerator/ORMappingGenerator/bin/PersistLayer.DLL"
>
> t ==t2
> true
>
> --------------------------------------------------------------------------------------------
>
> Any body can tell me why?
> thanks
>
> WangHF


Jul 30 '05 #4

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

Similar topics

1
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 ...
4
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
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...
0
by: Ray Cassick \(Home\) | last post by:
Ok, I am about at the end of my rope here. Been looking at this code for hours and can not see what is wrong with it. I have an object saved to a file using Binary Serialization. The file has...
2
by: JPerrin | last post by:
I'm attempting to create a generic data access layer that can load any assembly that has objects implementing the "IDb..." interfaces. Everything is working perfectly when I use a SqlConnection,...
3
by: WangHF | last post by:
hi , I had asked this question serveral days ago, but it's still unresolved. And I have found some new things. this error just occured sometimes,not always. the code like this:(It's running...
0
by: Devin | last post by:
I had built a COM+ object in VB.Net using EnterpriseServices. The COM+ object runs fine and interacts with the ASP.Net pages fine. I do get a weird error about once a day: ...
6
by: dgleeson3 | last post by:
Hello All I have VB code (.Net 2005) reading from an SQL server 2005 database. Im getting InvalidCastException when doing reader.GetInt32(0) Im simply reading an int from a simple database. It...
8
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
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...

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.