/// <summary> /// Check if the fields of <typeparamref name="T"/> can be copied. /// This method will throw an exception if copy cannot be performed for <typeparamref name="T"/> /// Read the exception message for detailed instructions about what is wrong. /// Use this to fail fast or in unit tests. /// </summary> /// <typeparam name="T">The type to get ignore fields for settings for</typeparam> /// <param name="referenceHandling"> /// If Structural is used property values for sub properties are copied for the entire graph. /// Activator.CreateInstance is sued to new up references so a default constructor is required, can be private /// </param> /// <param name="bindingFlags">The binding flags to use when getting fields</param> public static void VerifyCanCopyFieldValues <T>( ReferenceHandling referenceHandling = ReferenceHandling.Structural, BindingFlags bindingFlags = Constants.DefaultFieldBindingFlags) { var settings = FieldsSettings.GetOrCreate(referenceHandling, bindingFlags); VerifyCanCopyFieldValues <T>(settings); }
/// <summary> /// Compares x and y for equality using field values and returns the difference. /// If a type implements IList the items of the list are compared /// </summary> /// <typeparam name="T">The type of <paramref name="x"/> and <paramref name="y"/></typeparam> /// <param name="x">The first instance</param> /// <param name="y">The second instance</param> /// <param name="settings">Specifies how equality is performed.</param> /// <returns>Diff.Empty if <paramref name="x"/> and <paramref name="y"/> are equal</returns> public static Diff FieldValues <T>(T x, T y, FieldsSettings settings) { Ensure.NotNull(x, nameof(x)); Ensure.NotNull(y, nameof(y)); Ensure.NotNull(settings, nameof(settings)); EqualBy.Verify.CanEqualByMemberValues(x, y, settings, typeof(DiffBy).Name, nameof(FieldValues)); return(TryCreateValueDiff(x, y, settings) ?? new EmptyDiff(x, y)); }
/// <summary> /// Copies field values from source to target. /// Event fields are excluded /// </summary> /// <typeparam name="T">The type to get ignore fields for settings for</typeparam> /// <param name="source">The instance to copy field values from</param> /// <param name="target">The instance to copy field values to</param> /// <param name="settings">Contains configuration for how to copy</param> public static void FieldValues <T>(T source, T target, FieldsSettings settings) where T : class { Ensure.NotNull(source, nameof(source)); Ensure.NotNull(target, nameof(target)); Ensure.SameType(source, target, nameof(source), nameof(target)); Verify.CanCopyRoot(typeof(T), settings); Sync(source, target, settings); }
/// <summary> /// Compares x and y for equality using field values. /// If a type implements IList the items of the list are compared /// </summary> /// <typeparam name="T">The type to compare</typeparam> /// <param name="x">The first instance</param> /// <param name="y">The second instance</param> /// <param name="referenceHandling"> /// If Structural is used a deep equals is performed. /// Default value is Throw /// </param> /// <param name="bindingFlags">The binding flags to use when getting properties</param> /// <returns>Diff.Empty if <paramref name="x"/> and <paramref name="y"/> are equal</returns> public static Diff FieldValues <T>( T x, T y, ReferenceHandling referenceHandling = ReferenceHandling.Structural, BindingFlags bindingFlags = Constants.DefaultFieldBindingFlags) { var settings = FieldsSettings.GetOrCreate(referenceHandling, bindingFlags); return(FieldValues(x, y, settings)); }
/// <summary> /// Copies field values from source to target. /// Event fields are excluded /// </summary> /// <typeparam name="T">The type to get ignore fields for settings for</typeparam> /// <param name="source">The instance to copy field values from</param> /// <param name="target">The instance to copy field values to</param> /// <param name="referenceHandling"> /// If Structural is used field values for sub fields are copied for the entire graph. /// Activator.CreateInstance is sued to new up references so a default constructor is required, can be private /// </param> /// <param name="bindingFlags">The binding flags to use when getting properties</param> public static void FieldValues <T>( T source, T target, ReferenceHandling referenceHandling = ReferenceHandling.Structural, BindingFlags bindingFlags = Constants.DefaultFieldBindingFlags) where T : class { var settings = FieldsSettings.GetOrCreate(referenceHandling, bindingFlags); FieldValues(source, target, settings); }
/// <summary> /// Create the settings object. /// </summary> /// <param name="referenceHandling">How references are handled.</param> /// <param name="bindingFlags">What bindingflags to use</param> /// <returns>An instance of <see cref="FieldsSettings"/></returns> public FieldsSettings CreateSettings( ReferenceHandling referenceHandling = ReferenceHandling.Structural, BindingFlags bindingFlags = Constants.DefaultFieldBindingFlags) { if (this.ignoredFields.Count == 0 && this.ignoredTypes == null && this.comparers.Count == 0 && this.copyers.Count == 0) { return(FieldsSettings.GetOrCreate(referenceHandling, bindingFlags)); } return(new FieldsSettings( this.ignoredFields, this.ignoredTypes, this.immutableTypes, this.comparers, this.copyers, referenceHandling, bindingFlags)); }
/// <summary> /// Check if the fields of <typeparamref name="T"/> can be copied. /// This method will throw an exception if copy cannot be performed for <typeparamref name="T"/> /// Read the exception message for detailed instructions about what is wrong. /// Use this to fail fast or in unit tests. /// </summary> /// <typeparam name="T">The type to get ignore fields for settings for</typeparam> /// <param name="settings">Contains configuration for how copy is performed</param> public static void VerifyCanCopyFieldValues <T>(FieldsSettings settings) { var type = typeof(T); VerifyCanCopyFieldValues(type, settings); }
/// <summary> /// Check if the fields of <paramref name="type"/> can be copied. /// This method will throw an exception if copy cannot be performed for <paramref name="type"/> /// Read the exception message for detailed instructions about what is wrong. /// Use this to fail fast or in unit tests. /// </summary> /// <param name="type">The type to get ignore fields for settings for</param> /// <param name="settings">Contains configuration for how copy is performed</param> public static void VerifyCanCopyFieldValues(Type type, FieldsSettings settings) { Verify.CanCopyRoot(type, settings); Verify.CanCopyMemberValues(type, settings, typeof(Copy).Name, nameof(VerifyCanCopyFieldValues)); }
/// <summary> /// Check if the fields of <typeparamref name="T"/> can be compared for equality /// This method will throw an exception if copy cannot be performed for <typeparamref name="T"/> /// Read the exception message for detailed instructions about what is wrong. /// Use this to fail fast or in unit tests. /// </summary> /// <typeparam name="T">The type to check.</typeparam> /// <param name="settings">The settings to use.</param> public static void VerifyCanDiffByFieldValues <T>(FieldsSettings settings) { EqualBy.Verify.CanEqualByMemberValues <T>(settings, typeof(DiffBy).Name, nameof(FieldValues)); }
/// <summary> /// Check if the fields of <paramref name="type"/> can be compared for equality /// This method will throw an exception if copy cannot be performed for <paramref name="type"/> /// Read the exception message for detailed instructions about what is wrong. /// Use this to fail fast or in unit tests. /// </summary> /// <param name="type">The type to check.</param> /// <param name="settings">The settings to use.</param> public static void VerifyCanEqualByFieldValues(Type type, FieldsSettings settings) { Verify.CanEqualByMemberValues(type, settings, typeof(EqualBy).Name, nameof(EqualBy.FieldValues)); }
/// <summary> /// Compares x and y for equality using field values. /// If a type implements IList the items of the list are compared. /// Event fields are excluded. /// For performance the overload with settings should be used and the settings should be cached. /// </summary> /// <typeparam name="T">The type of <paramref name="x"/> and <paramref name="y"/></typeparam> /// <param name="x">The first instance</param> /// <param name="y">The second instance</param> /// <param name="settings">Specifies how equality is performed.</param> /// <returns>True if <paramref name="x"/> and <paramref name="y"/> are equal</returns> public static bool FieldValues <T>(T x, T y, FieldsSettings settings) { return(MemberValues(x, y, settings)); }