How do you do a deep copy of an object in .NET (C# specifically)? -
this question has answer here:
- deep cloning objects 37 answers
i want true deep copy. in java, easy, how do in c#?
i've seen few different approaches this, use generic utility method such:
public static t deepclone<t>(t obj) { using (var ms = new memorystream()) { var formatter = new binaryformatter(); formatter.serialize(ms, obj); ms.position = 0; return (t) formatter.deserialize(ms); } }
notes:
- your class must marked
[serializable]
in order work. your source file must include following code:
using system.runtime.serialization.formatters.binary; using system.io;
Comments
Post a Comment