using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;
publicstaticclassObjectCopier { publicstatic T Clone<T>(T source) { if (!typeof(T).IsSerializable) { thrownew ArgumentException("The type must be serializable.", nameof(source)); }
if (ReferenceEquals(source, null)) returndefault;
usingvar stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } }
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary;
publicstaticclassObjectCopier { publicstatic T Clone<T>(T source) { if (!typeof(T).IsSerializable) { thrownew ArgumentException("The type must be serializable.", nameof(source)); }
if (ReferenceEquals(source, null)) returndefault;
usingvar stream = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } }