An InvalidCastException again

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
fals
-------------------------------------------------------------------------------------------



---------------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
 
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.Mapping));

(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.
 
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?
 
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.
 
Back
Top