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.
edited for answer, (Thanks @Greg Beech) This worked:-
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
查看BindingFlags.DeclaredOnly
并将其传递给GetProperties
(您可能希望至少将它与BindingFlags.Public
和BindingFlags.Instance
结合起来)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.