private void ClonePublicFieldsProperties(object source, object dest) { var fields = source.GetType().GetFields().Where(fi => fi.IsPublic); foreach (var f in fields) { var srcValue = f.GetValue(source); f.SetValue(dest, factory.CloneObject(srcValue, f.GetCustomAttributes <CloneableAttribute>())); } var properties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where( pi => pi.CanWrite && pi.GetIndexParameters().Count() == 0); foreach (var p in properties) { var srcValue = p.GetValue(source); p.SetValue(dest, factory.CloneObject(srcValue, p.GetCustomAttributes <CloneableAttribute>())); } }
public object DeepClone(object source, ICloningFactory factory) { var sourceArray = source as Array; var clonedArray = (source as Array).Clone() as Array; factory.RegisterClonedObject(source, clonedArray); for (int i = 0; i < sourceArray.Length; i++) { var sourceValue = sourceArray.GetValue(i); clonedArray.SetValue(factory.CloneObject(sourceArray.GetValue(i), CloningMode.Deep), i); } return(clonedArray); }
public object DeepClone(object source, ICloningFactory factory) { var sourceList = source as IList; var clonedList = Activator.CreateInstance(source.GetType()) as IList; factory.RegisterClonedObject(source, clonedList); for (int i = 0; i < sourceList.Count; i++) { var sourceValue = sourceList[i]; clonedList.Insert(i, factory.CloneObject(sourceList[i], CloningMode.Deep)); } return(clonedList); }
public T Clone <T>(T source) { cloningFactory.Reset(); SetupCloningModeOverrides(source); return((T)cloningFactory.CloneObject(source, CloningMode.Shallow)); }