You should use the Type.GetProperty(String, BindingFlags)
method overload and include BindingFlags.DeclaredOnly
to restrict the search to members that are not inherited.
Read more here: http://msdn.microsoft.com/en-us/library/kz0a8sxy(v=vs.110).aspx
In response to the comment (even though I'm not sure if I understand it correctly). Here is a quick program which will traverse the type hierarchy and display the properties declared on the type itself, excluding inherited ones at each level.
namespace ConsoleApplication1
{
class ClassA
{
public string ClassAProp { get; set; }
}
class ClassB : ClassA
{
public string ClassBProp { get; set; }
}
class ClassC : ClassB
{
public string ClassCProp { get; set; }
}
class Program
{
static void Main(string[] args)
{
var c = new ClassC();
var t = c.GetType();
while (t.BaseType != null)
{
var cProps = t.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
foreach (var p in cProps)
{
Console.WriteLine("{0} defines {1}", t.Name, p.Name);
}
t = t.BaseType;
}
Console.ReadLine();
}
}
}
So, you can see 'BindingFlags.DeclaredOnly' returns only the properties declared on the class. Hope this helps to achieve what you are looking for.
Note: Type.BaseType
will be null for the object
type, as it's the root type. If you want the loop to stop at any other type, you should be able to say while (t != TypeYouWantToStopAt.GetType())