Testing 456

May 21, 2010 19:58

///
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
///

public static class ObjectCopier
{
///
/// Perform a deep Copy of the object.
///
/// The type of object being copied.
///
The object instance to copy.
/// The copied object.
public static T Clone(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}

// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}

IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}

//Wondering if the formatting for posted code will stay in tact here.
//It doesn't on youtube. Which annoys me very much. I tried to put my
//code in the comments of a video.

//Ok. Next question...Embed test.

image Click to view

tech test

Previous post
Up