Deserialize method that "loads" the class instance itself: how???

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bob Rock (nospam.yet_another_apprentice_at_hotmail.com)
Date: 05/14/04


Date: Fri, 14 May 2004 13:57:33 +0200

Hello,

I've got an xml stream that I'd need to deserialize into an instance of a
given class A. I'd like to create an instance method on class A (method
Deserialize) that takes this XML stream as input and deserializes it "into
itself" ... in other words I'd like it to "fill" the instance of class A on
which the method has been called instead of returning another instance of
class A.

The code below gives a good idea of what I'd like:

public class A
{
    public void Deserialize(MemoryStream stream)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(A));

        // the following line of code obviously does not work since this is
readonly
        // but it gives a good idea of what I'd like to do
        this = serializer.Deserialize(stream);
    }
}

How can I accomplish this without having to manually "load" all the class
fields???

Bob Rock



Relevant Pages