public void ShouldReturnObjectFieldsExcludingBaseType() { // given var obj = new ComplexObjectWithFields(id: 1, name: "Test", value: 15, date: new DateTime(2015, 4, 21, 0, 0, 0)); // when var fields = Reflections.Fields(obj: obj, includeBase: false); // then Check.That(fields.OrderedKeys).ContainsExactly("id"); Check.That(fields.Get("id")).IsEqualTo(1); }
public static int FieldsHash(this object obj) { var fields = Reflections.Fields(obj); var hashCode = 1; foreach (var key in fields.OrderedKeys) { var value = fields.GetIfPresent(key); hashCode = 31 * hashCode + Objects.HashCode(value); } return(hashCode); }
/// <summary> /// Generated object hashCode from fields. /// Collections are supported. /// </summary> /// <param name="source">this</param> /// <returns>hashCode</returns> public static int FieldsHash(this object source) { Preconditions.IsNotNull(source, () => new ArgumentNullException("source")); var fields = Reflections.Fields(source); var hashCode = 1; foreach (var key in fields.OrderedKeys) { var value = fields.GetIfPresent(key); hashCode = 31 * hashCode + Objects.HashCode(value); } return(hashCode); }
public void ShouldReturnObjectFields() { // given var obj = new ObjectWithFields(name: "Test", value: 15, date: new DateTime(2015, 4, 21, 0, 0, 0)); // when var fields = Reflections.Fields(obj); // then Check.That(fields.OrderedKeys).ContainsExactly("name", "value", "date"); Check.That(fields.Get("name")).IsEqualTo("Test"); Check.That(fields.Get("value")).IsEqualTo(15); Check.That(fields.Get("date")).IsEqualTo(new DateTime(2015, 4, 21, 0, 0, 0)); }
public void ShouldReturnObjectFieldsIncludingBaseType() { // given var obj = new ComplexObjectWithFields(id: 1, name: "Test", value: 15, date: null); // when var fields = Reflections.Fields(obj: obj, includeBase: true); // then Check.That(fields.OrderedKeys).ContainsExactly("id", "name", "value", "date"); Check.That(fields.Get("id")).IsEqualTo(1); Check.That(fields.Get("name")).IsEqualTo("Test"); Check.That(fields.Get("value")).IsEqualTo(15); Check.That(fields.GetIfPresent("date")).IsNull(); }
public static bool EqualsByFields(this object obj, object that) { var type = obj.GetType(); if (that != null && that.GetType() == type) { var thisFields = Reflections.Fields(obj); var thatFields = Reflections.Fields(that); foreach (var key in thisFields.OrderedKeys) { var thisFieldValue = thisFields.GetIfPresent(key); var thatFieldValue = thatFields.GetIfPresent(key); if (!Objects.Equal(thisFieldValue, thatFieldValue)) { return(false); } } return(true); } return(false); }
/// <summary> /// Checks object equality by comparing fields. /// Collections are supported. /// </summary> /// <param name="source">this</param> /// <param name="that">compared object</param> /// <returns>true if objects fields are equals</returns> public static bool EqualsByFields(this object source, object that) { Preconditions.IsNotNull(source, () => new ArgumentNullException("source")); var type = source.GetType(); if (that != null && that.GetType() == type) { var thisFields = Reflections.Fields(source); var thatFields = Reflections.Fields(that); foreach (var key in thisFields.OrderedKeys) { var thisFieldValue = thisFields.GetIfPresent(key); var thatFieldValue = thatFields.GetIfPresent(key); if (!Objects.Equal(thisFieldValue, thatFieldValue)) { return(false); } } return(true); } return(false); }