Merge the state of two objects in C# using reflection

if (fromObject == null || toObject == null) { return; }

            PropertyInfo[] properties = typeof(T).GetProperties();
            object value = null;

            for (int i = 0; i < properties.Length; i++)
            {
                if (!properties[i].CanWrite) { continue; }

                value = null;
                IList list = properties[i].GetValue(fromObject, null) as IList;

                if (list != null)
                {
                    //Is a list.
                    value = list;
                }
                else
                {
                    value = properties[i].GetValue(fromObject, null);
                }

                if (doNotMergeIfValueNull && value == null)
                {
                    //do nothing.
                }
                else
                {
                    properties[i].SetValue(toObject, value, null);
                }
            }

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

Enable Visual Studio to use more than 2GB of memory