Exemplo n.º 1
0
            /// <summary>
            /// Make a clone of the current instance.
            /// </summary>
            /// <returns></returns>
            public GenericConfigEntry Clone()
            {
                // get list of public fields
                Type myType = this.GetType();

                FieldInfo[] myField = myType.GetFields();

                // a new copy for the clone
                GenericConfigEntry newCopy = (GenericConfigEntry)Activator.CreateInstance(myType);

                // copy values on those with missing values
                foreach (var field in myField)
                {
                    string name  = field.Name;
                    string value = (string)myType.GetField(name).GetValue(this);
                    myType.GetField(name).SetValue(newCopy, value);
                }

                return(newCopy);
            }
Exemplo n.º 2
0
            /// <summary>
            /// Add missing values from mergeFrom to this.
            /// </summary>
            /// <param name="mergeFrom"></param>
            public GenericConfigEntry Merge(GenericConfigEntry mergeFrom, bool overwrite = false)
            {
                // get list of public fields
                Type myType = mergeFrom.GetType();

                FieldInfo[] myField = myType.GetFields();

                // copy values on those with missing values
                foreach (var field in myField)
                {
                    string name = field.Name;
                    string from = (string)myType.GetField(name).GetValue(mergeFrom);
                    string to   = (string)myType.GetField(name).GetValue(this);
                    if (to == null || (overwrite && from != null))
                    {
                        myType.GetField(name).SetValue(this, from);
                    }
                }

                return(this);
            }