views:

70

answers:

1

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.

+3  A: 

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

Ants
Sorry! It does work.
Ngu Soon Hui