Dynamically copy a class from one instance to another using reflection

March 04, 2016

Occasionally, you find that you want to copy a class to another instance of itself. There are various reason, but typically the code can result in a `CopyTo()` function, with a long list of properties to be copied across. There’s nothing wrong with that and, performance-wise, it’s probably faster than what I’m suggesting in this post.

Here’s a generic method that will copy the class for you:



        private static void CopyClass<T>(T copyFrom, T copyTo)
        {
            if (copyFrom == null || copyTo == null)
                throw new Exception("Must not specify null parameters");

            var properties = copyFrom.GetType().GetProperties();

            foreach (var p in properties.Where(prop => prop.CanRead && prop.CanWrite))
            {
                object copyValue = p.GetValue(copyFrom);
                p.SetValue(copyTo, copyValue);
            }
        }

Nothing too onerous if you’ve ever seen reflection, but let’s quickly run through it. The first thing it does is ensure that both to destination and source are provided, then it gets a list of properties that can be read and written to, and copies the value across.

Obviously, this won’t include any private properties.

Here’s some test / example code for it. First, the class to copy:



    class TestClass
    {
        public string teststring { get; set; }

        public string mynextstring { get; set; }

        public void test()
        {

        }

        public int get1 { get { return 1; } }

        //public TestClass2 testClass2 { get; set; }
    }

And then tha calling code:




            TestClass tc = new TestClass()
            {
                //testClass2 = new TestClass2(),
                teststring = "test2123",
                mynextstring = "31"

            };

            var t = new TestClass();
            CopyClass<TestClass>(tc, t);
            Console.WriteLine(t.teststring);
            Console.WriteLine(t.mynextstring);
            //Console.WriteLine(t.testClass2.teststring);

            Console.ReadLine();


Okay, so that works; however, you’ll notice that I’ve included and commented out an external class reference. If you include this, then it will copy the class reference (not the class). What if we want to use this to copy child classes?

Actually, it’s not too complicated:



        private static void CopyClass<T>(T copyFrom, T copyTo, bool copyChildren)
        {
            if (copyFrom == null || copyTo == null)
                throw new Exception("Must not specify null parameters");

            var properties = copyFrom.GetType().GetProperties();

            foreach (var p in properties.Where(prop => prop.CanRead && prop.CanWrite))
            {
                if (p.PropertyType.IsClass && p.PropertyType != typeof(string))
                {
                    if (!copyChildren) continue;

                    var destinationClass = Activator.CreateInstance(p.PropertyType);
                    object copyValue = p.GetValue(copyFrom);

                    CopyClass(copyValue, destinationClass, copyChildren);

                    p.SetValue(copyTo, destinationClass);                  
                }
                else
                {
                    object copyValue = p.GetValue(copyFrom);
                    p.SetValue(copyTo, copyValue);
                }
            }
        }

There’s a couple of things to note here: firstly, we need to check if the value is a class, but exclude strings as a special case (I couldn’t find a neater way of doing this); secondly, create the destination class using reflection and cache the source value. Finally, we just recursively call the function, using generic type inference in order to supply the type (which we don’t have as a generic at this point).

If you now just uncomment out the references to TestClass2 it should recursively copy the class.

I have to be honest and say that I’m not too happy with the check for a string type, but other than that, this is quite a neat little way to copy a class.



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024