Click here to Skip to main content
Licence CPOL
First Posted 20 Apr 2008
Views 17,486
Downloads 127
Bookmarked 11 times

Object Cloning Using Generic in C#

This utility creates a new instance of your class using generic.
8 votes, 57.1%
1
4 votes, 28.6%
2
2 votes, 14.3%
3

4

5
1.76/5 - 14 votes
μ 1.76, σa 1.32 [?]

Introduction

In one of my project I came across a situation which I needed to make a clone of an object and unfortunately C# doesn’t have such utility or maybe there is and simply I don’t know yet! Anyway, first I started with MSDN and look at the ICloneable interface … and then I find some interesting articles like this one: Base class for cloning an object in C#. However I finally decided to write my own Cloning Class using generic and XmlSerializer! The CloneManager is a simple class that can create a new instance of any class. Moreover your class doesn’t require having any attribute decoration!

Using the code

All you have to do is to add reference to CloneManager project and create an instance of this class in your source code as shows below.

Code Example:

  //Create an instance of clone manager
  IClone cloneManager = new CloneManager();

  MyClass newInstance = cloneManager.Clone(instance);

Implementation

The CloneManager implements IClone interface which exposes only one method. The Clone method simply serialize the instance into a memory stream and deserialize this stream to a new instance of object using XmlSerializer and returns that to the caller.

    
public interface IClone 
{ 
    T Clone(T instance) where T : class; 
}



using System;
using System.Xml.Serialization;
using System.IO;

namespace Demo.Clone
{
    public class CloneManager:IClone
    {
        /// Clones the specified instance. 
        /// Returns a new instance of an object.
        T IClone.Clone(T instance){
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, instance);
            stream.Seek(0, SeekOrigin.Begin);
            return serializer.Deserialize(stream) as T;
        }
    }
}



Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kurush Rastkar

Software Developer

New Zealand New Zealand

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSilverlight PinmemberKvanttt15:00 21 May '11  
Generaluse this instead PinmemberJespr8:43 21 Apr '08  
GeneralRe: use this instead Pinmemberkurush13:21 21 Apr '08  
GeneralIt's plagiary from Lhotka CSLA.NET library (C# Bussiness Object book) PinmemberRoman Dinnik5:29 21 Apr '08  
GeneralThis will only work for a small number of classes Pinmember leppie 2:46 21 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 21 Apr 2008
Article Copyright 2008 by Kurush Rastkar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid