Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

With Type.GetProperties() you can retrieve all properties of your current class and the public properties of the base-class. Is it somehow possible to get the private properties of the base-class too?

class Base
{
    private string Foo { get; set; }
}

class Sub : Base
{
    private string Bar { get; set; }
}

Sub s = new Sub();
PropertyInfo[] pinfos = s.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
foreach (PropertyInfo p in pinfos)
{
    Console.WriteLine(p.Name);
}
Console.ReadKey();

This will only print "Bar" because "Foo" is in the base-class and private.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
332 views
Welcome To Ask or Share your Answers For Others

1 Answer

To get all properties (public/private/protected/internal/static/instance) of a given Type someType, you must access the base class by using someType.BaseType.

Example:

PropertyInfo[] props = someType.BaseType.GetProperties(
        BindingFlags.NonPublic | BindingFlags.Public
        | BindingFlags.Instance | BindingFlags.Static)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...