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

Firefox and Chrome dark mode

Dealing with the morons who built Dell 7710 and RAID

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