Ignore inherited Properties of a class does not work
See original GitHub issuepublic class Base
{
public int Id { get; set; }
}
public class Foo : Base
{
public string Name { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar : Base
{
public bool IsAvailable { get; set; }
public int Code { get; set; }
}
[Test]
public void CompareTests()
{
var item1 = new Foo
{
Id = 1,
Name = "Foo",
Bars = new List<Bar>
{
new Bar
{
Id = 1,
Code = 101,
IsAvailable = true
}
}
};
var item2 = new Foo
{
Id = 1,
Name = "Foo",
Bars = new List<Bar>
{
new Bar
{
Id = 3,
Code = 101,
IsAvailable = true
}
}
};
var config = new ComparisonConfig();
config.IgnoreProperty<Bar>(x => x.Id);
var comparer = new CompareLogic {Config = config};
Assert.IsTrue(comparer.Compare(item1, item2).AreEqual); //return false
}
In the example above, I am trying to compare 2 Foo types where Foo.Id must be the same and Bar.Id could be different.
It seems that IgnoreProperty methods does MembersToIgnore.Add(typeof(TClass).Name + "." + name);
but the ExcludeLogic.ShouldExcludeMember checks for config.MembersToIgnore.Contains(info.DeclaringType.Name + "." + info.Name);
where declaring type is my Base class.
I changed the info.DeclaringType.Name
with info.ReflectedType.Name
and all your tests passed
Maybe due to backward compatibility a new option to ComparisonConfig might be a solution. MemberInfo.ReflectedType is not available to .Net Standard 1.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
c# - How to hide an inherited property in a class without ...
10 Answers 10 · 5. With this method, doing something like ClassA.Name outside ClassA will expose the ClassBase.Name property. · 1. That is...
Read more >Why inherit a class without adding properties?
Using inheritance, there is no way to make both Three and Four work unless you start duplicating classes (which opens up a whole...
Read more >JsonIgnore attribute is not inherited in overridden properties
Description Marking a property in an abstract class as [JsonIgnore] does not get inherited when the property is overridden Configuration Which version of...
Read more >Serializing type which hides inherited property with both ...
First that two types cannot have the same name apparently. Second that it only looks at both types when both properties are set...
Read more >Cascade, specificity, and inheritance - Learn web development
Often, the problem is that you create two rules that apply different values of the same property to the same element.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can certainly create a PR that accounts for .NET Standard 1.3 and .NET Standard 2.0 by using compiler pre-processor directives.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if
Fixed with https://github.com/GregFinzer/Compare-Net-Objects/pull/178
Thanks!