ObjectsComparer icon indicating copy to clipboard operation
ObjectsComparer copied to clipboard

RecursiveComparison set to false makes the Compare method always return true in case of non-primitive (non-comparable) objects

Open Cosmatevs opened this issue 1 year ago • 1 comments

static void Main(string[] args)
{
    var o1 = new Class1 { Property1 = 1, Property2 = "5" };
    var o2 = new Class1 { Property1 = 2, Property2 = "5" };

    var comparer = new ObjectsComparer.Comparer<Class1>();
    comparer.Settings.RecursiveComparison = true;
    Console.WriteLine(comparer.Compare(o1, o2, out _)); // returns false, as expected

    comparer.Settings.RecursiveComparison = false;
    Console.WriteLine(comparer.Compare(o1, o2, out _)); // returns true

    Console.ReadLine();
}

public class Class1
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

I guess that it happens because the Compare method tries to compare the objects but doesn't try to compare its properties as it's treated as recursion. It makes this library useless in such cases.

I can see two solutions for this problem:

  • make the first recursion always occur,
  • add a settings parameter to define how deep recursion can go (in this case, it would be a value of 1).

Additional context: initially, I turned off recursion because objects I wanted to compare had circular references to other objects. Of course I could ignore those members but it's near a quarter of members of that class. Additionally, it would force me to manually exclude any new problematic members which might appear in the future.

Cosmatevs avatar Jul 31 '23 07:07 Cosmatevs

Sorry for the later response. I think you are right. I'll fix it in the next version.

ValeraT1982 avatar Oct 02 '23 23:10 ValeraT1982