/// <summary> /// Copies all dynamic properties from an instance to another instance. /// </summary> /// <param name="fromInstance">The instance to copy the shadow attributes from</param> /// <param name="toInstance">The instance to copy the shadow attributes to</param> public static void CopyDynamicProperties(object fromInstance, object toInstance) { if (fromInstance == null) { throw new ArgumentNullException(nameof(fromInstance)); } if (toInstance == null) { throw new ArgumentNullException(nameof(toInstance)); } var type = fromInstance.GetType().GetTypeInfo(); bool forceShadowCreation = IdentifiableHelper.IsIdentifiable(type); ShadowContainer shadow; if (forceShadowCreation) { shadow = Shadows.GetValue(fromInstance, callback => new ShadowContainer(fromInstance.GetType())); } else { Shadows.TryGetValue(fromInstance, out shadow); } if (shadow != null) { var newShadow = Shadows.GetValue(toInstance, key => new ShadowContainer()); shadow.CopyTo(newShadow); newShadow.SetId(toInstance, shadow.GetId(fromInstance)); } }
/// <summary> /// Copies all dynamic properties from an instance to another instance. /// </summary> /// <param name="fromInstance">The instance to copy the shadow attributes from</param> /// <param name="toInstance">The instance to copy the shadow attributes to</param> public static void Copy(object fromInstance, object toInstance) { if (!Enable) { return; } if (fromInstance == null) { throw new ArgumentNullException(nameof(fromInstance)); } if (toInstance == null) { throw new ArgumentNullException(nameof(toInstance)); } var type = fromInstance.GetType(); // If the type is identifiable, we need to force the creation of a ShadowObject in order to // generate an id bool forceShadowCreation = IdentifiableHelper.IsIdentifiable(type); ShadowObject shadow; if (forceShadowCreation) { shadow = Shadows.GetValue(fromInstance, callback => new ShadowObject(fromInstance.GetType())); } else { Shadows.TryGetValue(fromInstance, out shadow); } if (shadow != null) { var newShadow = Shadows.GetValue(toInstance, key => new ShadowObject()); shadow.CopyTo(newShadow); // Copy the id of the attached reference to the destination if (shadow.IsIdentifiable) { newShadow.SetId(toInstance, shadow.GetId(fromInstance)); } } }
private ShadowObject(Type type) { isIdentifiable = IdentifiableHelper.IsIdentifiable(type); }
public ShadowContainer(Type type) { isIdentifiable = IdentifiableHelper.IsIdentifiable(type); }