5,557,174 members and growing! (16,212 online)
Email Password   helpLost your password?
Languages » C# » How To     Beginner License: The Common Development and Distribution License (CDDL)

Object Cloning at its simplest

By Rahul D.

A reusable static class to clone objects, not specific to TYPE of the object
C# (C# 3.0, C# 1.0, C# 2.0, C#), .NET (.NET, .NET 3.0, .NET 2.0), Visual Studio (VS2005, Visual Studio), Dev

Posted: 28 Feb 2008
Updated: 28 Feb 2008
Views: 2,161
Bookmarked: 3 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 0.86 Rating: 1.43 out of 5
3 votes, 75.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 25.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This article is about Cloning an object without having to implement ICloneable. And this class can be reused in any (C#) projects because it can clone any object of any TYPE that you specify.

Background

After a huge overnight coding of my project, I suddenly realize, oh! such a big class with so many members. How do I clone it? and I haven't implemented my base class ICloneable. Then I googled and I found all examples showing how to implement ICloneable interface, in codeproject itself I found an article showing the way of using Serialization method. But then this method I had to copy-paste in all the classes. Then I thought of generic(template) class.

Using the code

The first step is to create class. In order to do that, we need to declare the class and create a method as a template/generic definition by adding the generic type parameter <T>

/*
 * By - Rahul Dantkale
 * Company - Indigo Architects
 * 
 */
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;     
      
public static class ObjectClone
{
    public static T Clone<T>(T RealObject)
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream, RealObject);
            objectStream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(objectStream);
        }
    }
}        

Note that, the object which can be cloned by this method, the type of that object has to be marked as Serializable like,

[Serializable] 
public class MyClassToClone() 
{
  ...

The reason is, to clone the object I am using Serialization method which serializes memory stream, read the binary data and de-serialize to form object, I typecast it and return the object.

Take a look at source code attached with the article, it's easy to digest.

Points of Interest

This made me to learn Serialization and deserialization using System.Runtime.Serialization and Binary formatter in System.Runtime.Serialization.Formatters.Binary libraries. Using this I am now able to zip in memory to save space. (probably you'll need other libraries to zip and unzip but this article is not about it).

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)

About the Author

Rahul D.


I am from Solapur, India. I acquired my Degree of Bachelor in 2007 from Pune University, in VP College Of Engg., Baramati.
Currently Learning Microsoft .net 3.0 technologies (WF, WCF, WPF). Apart from this, I love playing PC games. During my college days people use to call me nightmare in Half Life : Condition Zero

I keep interest in game coding as well.
Occupation: Software Developer
Company: Indigo Architects, Pune
Location: India India

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralA better waymemberHyperteX6:09 28 Feb '08  
AnswerRe: A better waymember-(|[Nightmare]|)-18:41 28 Feb '08  
AnswerRe: A better waymemberADLER122:20 28 Feb '08  
GeneralRe: A better waymemberRobert Rohde23:31 28 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Feb 2008
Editor:
Copyright 2008 by Rahul D.
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project