0

I have two classes

public class A
{
  public int BaseA
{get;set;}
}

public Class B: A
{
 public int BaseB
{get;set;}
}

I can get the Properties for the Class B by using typeof(B).GetProperties(). However, this would include both the BaseA and BaseB properties. But I want to obtain the BaseB property only.

Note: I found the solution, it's

  B boy = new B();
            var pList = boy.GetType().GetProperties(BindingFlags.Public |
                  BindingFlags.DeclaredOnly |
                  BindingFlags.Instance);
            Assert.AreEqual(1, pList.Length);

A similar solution can be found here.

1 Answer 1

3

Look at using BindingFlags.DeclaredOnly when calling Type.GetProperties().

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.