How do you do a deep copy of an object in .NET (C# specifically)? -


this question has answer here:

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

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -