/// <summary> /// Determines whether properties are initialized or not. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="propertiesContainer">The properties container.</param> /// <param name="trace">The trace.</param> /// <param name="options">The options.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// propertiesContainer - Cannot check if properties are initialized on null /// instance /// </exception> // ReSharper disable once ExcessiveIndentation public static bool PropertiesAreInitialized <T>(T propertiesContainer, CheckingTrace trace, PropertiesComparisonOptions options) { if (propertiesContainer == null) { throw new ArgumentNullException(nameof(propertiesContainer), "Cannot check if properties are initialized on null instance"); } var type = propertiesContainer.GetType(); var properties = ObjectComparer.SelectProperties(type.GetProperties(), options.ExcludedProperties); foreach (var propertyName in properties) { var property = type.GetProperty(propertyName); if (options.IgnoreReadOnlyProperties && !property.CanWrite) { continue; } if (property.CanRead) { // Indexed Properties are managed if (property.GetIndexParameters().Length == 0) { var value = property.GetValue(propertiesContainer, null); trace.PushProperty(type, property.PropertyType, propertyName); if (!IsFilled(value, trace, options)) { return(false); } trace.Pop(); } } } return(true); }
private static bool PropertiesAreEqual(object expected, object tested, IEnumerable <string> properties, PropertiesComparisonOptions comparisonOptions, CheckingTrace trace) { foreach (var propertyName in properties) { if (propertyName == "SyncRoot") { continue; } var expectedType = expected.GetType(); // get property infos var expectedProperty = expectedType.GetProperty(propertyName); var getter = expectedProperty.GetGetMethod(); if (getter == null || getter.IsStatic) { // no getter, or static continue; } var testedType = tested.GetType(); var testedProperty = testedType.GetProperty(expectedProperty.Name); if (testedProperty == null) { trace.SetFailure( $"{testedType}:{tested} has not the expected property {expectedProperty.Name} of {expectedType}"); return(false); } // compare values if (expectedProperty.CanRead && testedProperty.CanRead) { if (expectedProperty.GetIndexParameters().Length != 0) { continue; } var expectedValue = expectedProperty.GetValue(expected, null); var testedValue = testedProperty.GetValue(tested, null); trace.PushProperty(expectedType, expectedProperty.PropertyType, expectedProperty.Name); if (!ObjectsAreEqual(expectedValue, testedValue, comparisonOptions, trace)) { return(false); } trace.Pop(); } } return(true); }