4

Consider the following

I have an Html Template object

   class HtmlTemplateModel
    {
       public HTML Html {get;set;}
       //..etc
    }

and an Html Editor

class HtmlEditorViewModel
{
   private HtmlTemplateModel htmlTemplateModel;
   public HtmlEditor(HtmlTemplateModel htmlTemplateModel)
   {
      this.htmlTemplateModel = htmlTemplateModel;
   }
}

This way the TemplateEditor instance applies the changes to the original template since its a reference type, but i dont want to modify its value so i made a new constructor that takes an HtmlTemplateModel and creates a new HtmlTemplateModel.

  • is this the best way to clone reference type in .NET?
  • and is there a shortcut so that i don't have to create another constructor to clone the object?
FPGA
  • 3,525
  • 10
  • 44
  • 73

1 Answers1

2

A simple way is to serialize the object, it will make a deep copy of the source object.

http://www.codeproject.com/Articles/23832/Implementing-Deep-Cloning-via-Serializing-objects

T McKeown
  • 12,971
  • 1
  • 25
  • 32