1

I'm currently using FluentAssertion for comparing 2 objects.

I really want to know what is the way it uses to compare?

Using Reflection then loop all props like this?

public static void PropertyValuesAreEquals(object actual, object expected)   
{
        PropertyInfo[] properties = expected.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object expectedValue = property.GetValue(expected, null);
            object actualValue = property.GetValue(actual, null);
          if (!Equals(expectedValue, actualValue))
                Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue);
    //……………………………….
}

If it uses another way to compare, what is it?

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56

1 Answers1

5

I suggest you read the documentation to understand the algorithm it uses. But I can tell you it's full of Reflection.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • First of all, Allow me to say thank you for your answer. However, As far as I know: **The reflection mechanism is not as fast as it is in other solutions** Actually, I really wanna know what is different between "Object graph comparison" and ["Normal way to compare 2 objects like this" ](https://ibb.co/3RWFLFB) Talk about some aspects: 1 Performance: Which one is better? 2 The way FluentAssertion: What does make it better than another way? I mean whether each time I run Unit Test then it will loop throughout all properties to compare like the normal way that I just mentioned above? – Nguyễn Văn Phong Dec 09 '19 at 08:28
  • 1
    FA does a deep comparison. So it doesn't only look at the values of the properties, but also at the types of those properties, and will, depending on the types, use different strategies. See also https://github.com/fluentassertions/fluentassertions/blob/master/Src/FluentAssertions/EquivalencyStepCollection.cs#L115 – Dennis Doomen Dec 10 '19 at 13:07