Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Created May 9, 2013 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/5544814 to your computer and use it in GitHub Desktop.
Save pawelpabich/5544814 to your computer and use it in GitHub Desktop.
Deep clone
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Structure
{
public Structure()
{
DateTime = DateTime.Now;
Int = 12;
SubStructure = new SubStructure();
List = new[] {new SubStructure(), new SubStructure(), new SubStructure(), };
}
public int Int { get; set; }
public DateTime DateTime
{
get;
set;
}
public SubStructure SubStructure { get; set; }
public IEnumerable<SubStructure> List { get; set; }
}
public class SubStructure
{
public SubStructure()
{
String = Guid.NewGuid().ToString();
}
public String String { get; set; }
}
class Program
{
static void Main(string[] args)
{
var input = new Structure();
var serializer = new NetDataContractSerializer();
var stream = new MemoryStream();
serializer.Serialize(stream, input);
stream.Seek(0, SeekOrigin.Begin);
var copy = (Structure)serializer.Deserialize(stream);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment