我正在尝试读取给定对象的所有属性,只读取在对象类型上声明的属性,不包括继承的属性。即:
class Parent {
public string A { get; set; }
}
class Child : Parent {
public string B { get; set; }
}
所以我只想找回 B。阅读文档,我假设下面是我需要的,但实际上什么也没有返回。
var names = InstanceOfChild.GetType().GetProperties(BindingFlags.DeclaredOnly).Select(pi => pi.Name).ToList();
最佳答案
只需要几个其他的 BindingFlags
var names = InstanceOfChild.GetType().GetProperties(
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.Instance).Select(pi => pi.Name).ToList();
关于c# - 获取非继承属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5981417/