Hi!
I want to make a copy of an class, in this case: Model(XNA).
The problem is that when I edit one of my Models all of the other models of the same type gets changed as well. I can't seem to figure it out.
Xna's official site is down for another couple of days, so therefore the post this here.

Thanks!

(I posted again as it seems like my last post wasn't posted :P)

When using the built in Copy method, it does what is called a shallow copy. This means that anything inside the object that is a reference, they just copy the reference (they don't create a new object of that type).

You'll have to create your own copy method that does a deep copy. If the object is Serializable then you can use this extension method:

public static T DeepCopy<T>(T obj) {
    object result = null;

    using (var ms = new MemoryStream()) {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(ms, obj);
        ms.Position = 0;

        result = (T)formatter.Deserialize(ms);
        ms.Close();
    }

    return (T)result;
}

This, of course, will only copy those parts of the object that are themselves serializable.

commented: Nice way to do a deep copy! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.