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

Enable Visual Studio to use more than 2GB of memory

Firefox and Chrome dark mode

Dealing with the morons who built Dell 7710 and RAID