question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ignore inherited Properties of a class does not work

See original GitHub issue
public 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
GregFinzercommented, Oct 3, 2019

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

0reactions
GregFinzercommented, Oct 15, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found