Access to properties of abstract class with reflection

前端 未结 2 868
时光说笑
时光说笑 2021-01-21 09:23

I have an abstract class — let\'s name it Base. This class contains some properties. Moreover, I have another class, inherited from class Base — let\'s

相关标签:
2条回答
  • 2021-01-21 10:05

    Try this:

    Type type = typeof(A);
    
    PropertyInfo[] propInfos 
        = type.GetProperties(BindingFlags.Instance 
            | BindingFlags.Public 
            | BindingFlags.DeclaredOnly);
    
    0 讨论(0)
  • 2021-01-21 10:13

    Invoking GetType() on an object is only one of the ways of getting a Type object. Another, which works even for abstract classes, is typeof(). Using the BindingFlags.DeclaredOnly option with typeof(A).GetProperties should do the trick.

    0 讨论(0)
提交回复
热议问题