[英]How do I get the properties of an object using reflection?
I know I can do this我知道我可以做到这一点
foreach (PropertyInfo property in myobject.GetType().GetProperties())
{
if (property.DeclaringType.ToString() == myobject.GetType().ToString())
{
// only have my object properties here
// and not parent of my object properties
}
}
But how can I just get the properties of myobject and not those of the parent as well?但如何才能使我刚刚得到的MyObject的性质,而不是那些父母的呢? ie not have to do that extra if statement.
即不必做额外的 if 语句。
edited for answer, (Thanks @Greg Beech) This worked:-编辑答案,(感谢@Greg Beech)这有效:-
foreach (PropertyInfo property in
myobject.GetType().GetProperties
(BindingFlags.Public |
BindingFlags.DeclaredOnly |
BindingFlags.Instance))
{
// only properties of my object not parent of myobject
}
I also found this link http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx我还找到了这个链接http://msdn.microsoft.com/en-us/library/4ek9c21e.aspx
查看BindingFlags.DeclaredOnly
并将其传递给GetProperties
(您可能希望至少将它与BindingFlags.Public
和BindingFlags.Instance
结合起来)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.