0

I was curious to know if there was an easy way to do the following.

public class Person
{
    public String Name{ get; set; }

    public void Load(Stream stream)
    {
        this = new XmlSerializer(GetType()).Deserialize(stream) as Person;
    }

    public void Save(Stream stream)
    {
        new XmlSerializer(GetType()).Serialize(stream, this);
    }
}

I realize that this will not compile; however, I find that sometimes I wish to assign an object from within itself. (i.e. the object undergoes a massive change and I wish to instead of changing each value, simply "reset" the object by calling its constructor and setting the object to its new version).

Any thoughts on how to do this?

3
  • 1
    Instead of mutating and returning void return a newly created instance. Commented Jun 25, 2014 at 16:44
  • I do not understand... Do you mean like the singleton pattern which instantiates itself? Commented Jun 25, 2014 at 16:44
  • @asawyer in this particular case this is not possible because I wish to do this within a piece of code that implements an interface. Commented Jun 25, 2014 at 16:47

2 Answers 2

3

You could do this with Automapper that would just copy the contents of an instance into your instance:

public void Load(Stream stream)
{
    Mapper.DynamicMap( new XmlSerializer(GetType()).Deserialize(stream) as Person, this );
}

Nevertheless, I would prefer

public static Person Load(Stream stream)
{
    return new XmlSerializer(GetType()).Deserialize(stream) as Person;
}

so that instead of overwriting the contents, you return a new instance and could just switch references in the client code.

Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't appear OP is asking for Save(Stream stream) method to reset the contents, but rather asking for the Load(Stream stream) method to reassign "this" without having to write the code "this.X=deserialized.X; this.Y=deserialized.Y; this.Z=deserialized.Z..." (the Save() seems irrelevant to this question)
@Mick: the question has been edited since I answered it. The first version has Load/Save implemented in the opposite way it is now. Will edit my answer to reflect that.
Ack! You're edit was done while I was posting my answer. hangs head in shame
0

Just use a static method that returns a new instance:

public class Person
{
    public String Name { get; set; }

    public static Person Load(Stream stream)
    {
        return new XmlSerializer(GetType()).Deserialize(stream) as Person;
    }
}

Call it like this:

var person = Person.Load(someStream);

Edit: I didn't notice your requirement that this is in an interface implementation. If possible, you may want to refactor the design. If you can't and your object is simple enough, you could instantiate a new object and copy the members over:

public void Load(Stream stream)
{
    Person p = new XmlSerializer(GetType()).Deserialize(stream) as Person;
    // todo: copy properties from p to this:
    // this.Name = p.Name;
    // etc
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.