// Clones this instance and all its children. internal OptionsBase Clone() { OptionsBase options = (OptionsBase)Activator.CreateInstance(this.GetType()); options.CopyFrom(this); options._chart = this._chart; return(options); }
// Initializes this option set copying the value from another option set. internal virtual void CopyFrom(OptionsBase source) { if (source == null) { throw new ArgumentNullException("source"); } // copy only the shared base properties. var targetProperties = TypeDescriptor.GetProperties(this); var sourceProperties = TypeDescriptor.GetProperties(source); foreach (PropertyDescriptor pSource in sourceProperties) { var pTarget = targetProperties[pSource.Name]; if (pTarget == null || pTarget.PropertyType != pSource.PropertyType) { continue; } try { // go deep for nested options. if (pSource.PropertyType.IsSubclassOf(typeof(OptionsBase))) { // create a new instance of the child options member. OptionsBase options = (OptionsBase)Activator.CreateInstance(pSource.PropertyType, this); options.CopyFrom((OptionsBase)pSource.GetValue(source)); pTarget.SetValue(this, options); } else { if (pSource.ShouldSerializeValue(source)) { pTarget.SetValue(this, pSource.GetValue(source)); } } } catch { } } }