获取非继承属性
我试图读取给定对象的所有属性,仅读取在对象类型上声明的属性,不包括继承的属性。 IE:
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();
I'm trying to read all of the properties of a given object, reading in only those that are declared on the object's type, excluding those that are inherited. IE:
class Parent {
public string A { get; set; }
}
class Child : Parent {
public string B { get; set; }
}
And so I want to only get B back. Reading the docs, I assumed below was what I needed, but that actually returned nothing at all.
var names = InstanceOfChild.GetType().GetProperties(BindingFlags.DeclaredOnly).Select(pi => pi.Name).ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需要几个其他 BindingFlags
Just need a couple other BindingFlags
试试这个:
我将
BindingFlags.Instance
和BindingFlags.Public
添加到搜索参数中,根据 MSDN 文档 分别:和
Try this:
I added the
BindingFlags.Instance
andBindingFlags.Public
to the search parameters, which according to the MSDN documentation respectively:and