Click here to Skip to main content
11,812,712 members (72,161 online)
Click here to Skip to main content

Deep copy of objects in C#

, 18 Jul 2009 CPOL 76.1K 798 30
Rate this:
Please Sign up or sign in to vote.
How to do a deep copy of objects using System.Reflection.


Below you can find a short article on how to do a deep copy of objects using Reflection in C#. Please be aware that this is my first article here (even first article in the English language...)


The class (called HCloner) has a DeepCopy function. It drills down the entire object fields structure (using System.Reflection) and copies it into a new location that is returned after that.

Members that are copied are fields - no need to copy properties since behind every property, there is a field. A property itself cannot hold any value.

Using the code

Let's look at the "core" code:

using System;
using System.Reflection;

namespace HAKGERSoft {

    public class HCloner {

        public static T DeepCopy<T>(T obj) {
                throw new ArgumentNullException("Object cannot be null");
            return (T)Process(obj);

        static object Process(object obj) {
                return null;
            Type type=obj.GetType();
            if(type.IsValueType || type==typeof(string)) {
                return obj;
            else if(type.IsArray) {
                Type elementType=Type.GetType(
                var array=obj as Array;
                Array copied=Array.CreateInstance(elementType,array.Length);
                for(int i=0; i<array.Length; i++) {
                return Convert.ChangeType(copied,obj.GetType());
            else if(type.IsClass) {
                object toret=Activator.CreateInstance(obj.GetType());
                FieldInfo[] fields=type.GetFields(BindingFlags.Public| 
                foreach(FieldInfo field in fields) {
                    object fieldValue=field.GetValue(obj);
                return toret;
                throw new ArgumentException("Unknown type");


Using it is very simple - just call the DeepCopy function.


This is a very alpha-pre version of my function. I'm looking forward for some feedback from you - any issues found will be analysed and fixed (at least, I'll try).


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


About the Author

Software Developer (Senior)
Poland Poland
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionError at "return Convert.ChangeType(copied,obj.GetType() ); " Pin
VigneshPT24-Oct-11 10:36
memberVigneshPT24-Oct-11 10:36 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 18 Jul 2009
Article Copyright 2009 by Hakger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid