ValeraT1982 / ObjectsComparer

C# Framework provides mechanism to compare complex objects, allows to override comparison rules for specific properties and types.
MIT License
352 stars 86 forks source link

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

Open Cosmatevs opened 1 year ago

Cosmatevs commented 1 year ago
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:

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.

ValeraT1982 commented 11 months ago

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