public static T GetCopyOf <T>(T source) where T : class, new() { T target = new NullNormalizeFactory <T>(_ => { }).Instance; Reflector.Merge(target, source); return(target); }
/// <summary> /// Sets the reference of a source object to a new one with the copy of all its properties /// </summary> /// <typeparam name="T"></typeparam> /// <param name="source">the object to have a shalowed copy with</param> /// <returns></returns> public static T ReferShalowedCopy <T>(T source) where T : class, new() { T target = new NullNormalizeFactory <T>().Instance; Merge(target, source); return(target); }
/// <summary> /// Get a modifiable version of an object. Database objects with EF /// need to use copy to be modified if you plan on manipulating keys. /// here you got a semi transiant object that is the copy of what entity /// you want but not linked to the database since it is a new object /// null - normalized, merged with existant data and references /// </summary> /// <typeparam name="T"></typeparam> /// <param name="action">Use that to initialize the new instance</param> /// <param name="source">The object to merge with the target</param> /// <returns></returns> public static T GetModifiable <T>(T source, Action <T> action) where T : class, new() { // get a new instance of a class T where every nullable Members // are set to null value ... to make merging more compliant T target = new NullNormalizeFactory <T>(action).Instance; // If there are collisions for properties, keep target values var mergeType = Reflector.MergeOptions.KEEP_TARGET; // Merge the target with the source. Reflector.Merge(target, source, mergeType); return(target); }