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? ie not have to do that extra if statement.
但是,我怎样才能获得myobject的属性而不是父级的属性呢?即不必做额外的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
1 个解决方案
#1
3
Check out BindingFlags.DeclaredOnly
and pass that to GetProperties
(you'll probably want to combine it with BindingFlags.Public
and BindingFlags.Instance
at least).
查看BindingFlags.DeclaredOnly并将其传递给GetProperties(您可能希望至少将它与BindingFlags.Public和BindingFlags.Instance结合使用)。
#1
3
Check out BindingFlags.DeclaredOnly
and pass that to GetProperties
(you'll probably want to combine it with BindingFlags.Public
and BindingFlags.Instance
at least).
查看BindingFlags.DeclaredOnly并将其传递给GetProperties(您可能希望至少将它与BindingFlags.Public和BindingFlags.Instance结合使用)。