المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : XmlSerializer - usage within constructors



C# Programming
01-21-2010, 08:21 AM
Hello Experts,

I would like to useobject System.Xml.Serialization.XmlSerializer.Deserialize(System.IO.StreamReader stream)within a constructorpublic class MyObject()
{
/*
* A lot of data to be stored
*/

public MyObject (string fileName)
{
System.IO.StreamReader readingStream = new System.IO.StreamReader(fileName);
System.Xml.Serialization.XmlSerializer readingSerializer = new System.Xml.Serialization.XmlSerializer(typeof(MyObject));

// Deserialize file to get an instance of MyObject
MyObject justRead = (MyObject)readingSerializer.Deserialize(readingStream);
}
}But I don't know what to do with justRead.
Of course, I could just forget about the constructor and use a methodpublic static MyObject Load (string fileName)

But is it really impossible to create something usable like this one?MyObject iLoadedThisFromFile = new MyObject (storageFileName);


Ciao,


luker