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
Check out
BindingFlags.DeclaredOnlyand pass that toGetProperties(you’ll probably want to combine it withBindingFlags.PublicandBindingFlags.Instanceat least).