471,349 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 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 2522
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by JPerrin | last post: by
3 posts views Thread by WangHF | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.