473,240 Members | 1,884 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,240 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 2600
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.