/// <summary> /// Creates a duplicate of this descriptor using MemberwiseClone /// </summary> /// <returns>A clone of this object as a duplicate</returns> object ICloneable.Clone() { ProjDescriptor copy = MemberwiseClone() as ProjDescriptor; OnCopy(copy); return(copy); }
/// <summary> /// This occurs during the Copy method and is overridable by sub-classes /// </summary> /// <param name="copy">The duplicate descriptor</param> protected virtual void OnCopy(ProjDescriptor copy) { // This checks any property on copy, and if it is cloneable, it // creates a clone instead Type copyType = copy.GetType(); PropertyInfo[] copyProperties = DistinctNames(copyType.GetProperties(BindingFlags.Public | BindingFlags.Instance)); PropertyInfo[] myProperties = DistinctNames(GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)); foreach (PropertyInfo p in copyProperties) { if (p.CanWrite == false) { continue; } if (myProperties.Contains(p.Name) == false) { continue; } PropertyInfo myProperty = myProperties.GetFirst(p.Name); object myValue = myProperty.GetValue(this, null); if (myProperty.GetCustomAttributes(typeof(ProjShallowCopy), true).Length > 0) { // This property is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) { continue; } p.SetValue(copy, cloneable.Clone(), null); } FieldInfo[] copyFields = copyType.GetFields(BindingFlags.Public | BindingFlags.Instance); FieldInfo[] myFields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo f in copyFields) { if (myFields.Contains(f.Name) == false) { continue; } FieldInfo myField = myFields.GetFirst(f.Name); object myValue = myField.GetValue(copy); if (myField.GetCustomAttributes(typeof(ProjShallowCopy), true).Length > 0) { // This field is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) { continue; } f.SetValue(copy, cloneable.Clone()); } }
/// <summary> /// This occurs during the Copy method and is overridable by sub-classes /// </summary> /// <param name="copy">The duplicate descriptor</param> protected virtual void OnCopy(ProjDescriptor copy) { // This checks any property on copy, and if it is cloneable, it // creates a clone instead Type copyType = copy.GetType(); PropertyInfo[] copyProperties = DistinctNames(copyType.GetProperties(BindingFlags.Public | BindingFlags.Instance)); PropertyInfo[] myProperties = DistinctNames(GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)); foreach (PropertyInfo p in copyProperties) { if (p.CanWrite == false) continue; if (myProperties.Contains(p.Name) == false) continue; PropertyInfo myProperty = myProperties.GetFirst(p.Name); object myValue = myProperty.GetValue(this, null); if (myProperty.GetCustomAttributes(typeof(ProjShallowCopy), true).Length > 0) { // This property is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) continue; p.SetValue(copy, cloneable.Clone(), null); } FieldInfo[] copyFields = copyType.GetFields(BindingFlags.Public | BindingFlags.Instance); FieldInfo[] myFields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo f in copyFields) { if (myFields.Contains(f.Name) == false) continue; FieldInfo myField = myFields.GetFirst(f.Name); object myValue = myField.GetValue(copy); if (myField.GetCustomAttributes(typeof(ProjShallowCopy), true).Length > 0) { // This field is marked as shallow, so skip cloning it continue; } ICloneable cloneable = myValue as ICloneable; if (cloneable == null) continue; f.SetValue(copy, cloneable.Clone()); } }