forked from MikeG621/Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectCopier.cs
More file actions
45 lines (42 loc) · 1.47 KB
/
Copy pathObjectCopier.cs
File metadata and controls
45 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Idmr.Common.dll, Library file with common IDMR resources
* Copyright (C) 2007-2014 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in help/Idmr.Common.chm
* Version: 1.3
*/
/* CHANGE LOG
* v1.3, 141214
* - Release
*/
using System;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Idmr.Common
{
/// <summary>Class to provide a deep copy extension method.</summary>
public static class ObjectCopier
{
/// <summary>Returns a deep copy of the object</summary>
/// <typeparam name="T">The type of the object being cloned.</typeparam>
/// <param name="original">The object to be cloned.</param>
/// <exception cref="System.ArgumentException"><i>original</i> is not a serializable object.</exception>
/// <returns>The cloned object.</returns>
/// <remarks>This is an extension method that provides deep copy functionality to any object. Object must be serializable.</remarks>
public static T DeepClone<T>(this T original) where T : class
{
if (!typeof(T).IsSerializable)
throw new ArgumentException("original must be serializable.", "original");
if (Object.ReferenceEquals(original, null)) return default(T);
using (MemoryStream stream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, original);
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}
}
}
}