public static int PropertiesHash(this object obj) { var properties = Reflections.Properties(obj); var hashCode = 1; foreach (var key in properties.OrderedKeys) { var value = properties.GetIfPresent(key); hashCode = 31 * hashCode + Objects.HashCode(value); } return(hashCode); }
/// <summary> /// Generates object hashCode from properties. /// Collections are supported. /// </summary> /// <param name="source">this</param> /// <returns>hashCode</returns> public static int PropertiesHash(this object source) { Preconditions.IsNotNull(source, () => new ArgumentNullException("source")); var properties = Reflections.Properties(source); var hashCode = 1; foreach (var key in properties.OrderedKeys) { var value = properties.GetIfPresent(key); hashCode = 31 * hashCode + Objects.HashCode(value); } return(hashCode); }
public void ShouldReturnObjectPropertiesIncludingBaseType() { // given const int secret = 33; var testObject = new NestedTypeWithProperties(id: 1, secret: secret, value: 5, name: "x", extension: "abc"); // when var properties = Reflections.Properties(obj: testObject, includeBase: true); // then Check.That(properties.OrderedKeys).ContainsExactly("Extension", "Id", "Name", "Value", "Secret"); Check.That(properties.Get("Id")).IsEqualTo(testObject.Id); Check.That(properties.Get("Name")).IsEqualTo(testObject.Name); Check.That(properties.Get("Value")).IsEqualTo(testObject.Value); Check.That(properties.Get("Secret")).IsEqualTo(secret); Check.That(properties.Get("Extension")).IsEqualTo(testObject.Extension); }
public static bool EqualsByProperties(this object obj, object that) { var type = obj.GetType(); if (that != null && that.GetType() == type) { var thisProperties = Reflections.Properties(obj); var thatProperties = Reflections.Properties(that); foreach (var key in thisProperties.OrderedKeys) { var thisPropertyValue = thisProperties.GetIfPresent(key); var thatPropertyValue = thatProperties.GetIfPresent(key); if (!Objects.Equal(thisPropertyValue, thatPropertyValue)) { return(false); } } return(true); } return(false); }
/// <summary> /// Checks object equality by comparing properties. /// Collections are supported. /// </summary> /// <param name="source">this</param> /// <param name="that">compared object</param> /// <returns>true if objects properties are equals</returns> public static bool EqualsByProperties(this object source, object that) { Preconditions.IsNotNull(source, () => new ArgumentNullException("source")); var type = source.GetType(); if (that != null && that.GetType() == type) { var thisProperties = Reflections.Properties(source); var thatProperties = Reflections.Properties(that); foreach (var key in thisProperties.OrderedKeys) { var thisPropertyValue = thisProperties.GetIfPresent(key); var thatPropertyValue = thatProperties.GetIfPresent(key); if (!Objects.Equal(thisPropertyValue, thatPropertyValue)) { return(false); } } return(true); } return(false); }
public void ShouldReturnObjectProperties() { // given const int secret = 33; var testObject = new SimpleTypeWithProperties { Id = 1, Name = "Test", Value = null }; testObject.SetSecret(secret); // when var properties = Reflections.Properties(testObject); // then Check.That(properties.OrderedKeys).ContainsExactly("Id", "Name", "Value", "Secret"); Check.That(properties.Get("Id")).IsEqualTo(testObject.Id); Check.That(properties.Get("Name")).IsEqualTo(testObject.Name); Check.That(properties.Get("Value")).IsEqualTo(testObject.Value); Check.That(properties.Get("Secret")).IsEqualTo(secret); }