ValeraT1982 / ObjectsComparer

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

Doesn't work with object of derived class #54

Closed NaveenkumarIyappan closed 1 year ago

NaveenkumarIyappan commented 1 year ago

The comparer doesn't work when object of derived class is used. Ex:

public class BaseClass
{
 public int Id {get; set;}
}

public class DerviedClass
{
 public string Name {get; set;}
}

Comparer works when BaseClass property is different

var obj1 = new DerivedClass { Id = 1, Name = Foo };
var  obj2 = new DerivedClass { Id = 2, Name = Foo };

var _comparer = new ObjectsComparer.Comparer<BaseClass>();
var equal = _comparer.Compare(obj1, obj2, out differencesList);
// equal is false

But when derived class property is different it doesn't

var obj1 = new DerivedClass { Id = 1, Name = Hello };
var  obj2 = new DerivedClass { Id = 1, Name = Foo };

var _comparer = new ObjectsComparer.Comparer<BaseClass>();
var equal = _comparer.Compare(obj1, obj2, out differencesList);
// equal is true

Is this a issue of it's by design?

reponemec commented 1 year ago

I think you have to write it like this:

var _comparer = new ObjectsComparer.Comparer<DerivedClass>();
ValeraT1982 commented 1 year ago

@reponemec is right. When you do Comparer() only members of T will be compared