Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Memory Class Reference

Additional static functions used to extend existing Memory support inside of Unity. More...

Static Public Member Functions

static T Clone< T > (this T source)
 Perform a deep Copy of the object. More...
 

Detailed Description

Additional static functions used to extend existing Memory support inside of Unity.

Definition at line 39 of file Memory.cs.

Member Function Documentation

static T Hydrogen.Memory.Clone< T > ( this T  source)
static

Perform a deep Copy of the object.

Returns
The copied object.
Template Parameters
TObject Type
Parameters
sourceThe object instance to copy.

Definition at line 47 of file Memory.cs.

48  {
49  if (!typeof(T).IsSerializable) {
50  throw new ArgumentException ("The type must be serializable.", "source");
51  }
52 
53  // Don't serialize a null object, simply return the default for that object
54  if (Object.ReferenceEquals (source, null)) {
55  return default(T);
56  }
57 
58  IFormatter formatter = new BinaryFormatter ();
59  Stream stream = new MemoryStream ();
60  using (stream) {
61  formatter.Serialize (stream, source);
62  stream.Seek (0, SeekOrigin.Begin);
63  return (T)formatter.Deserialize (stream);
64  }
65  }