Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one get Properties using Reflection, while ignoring the inherited properties?

Tags:

c#

reflection

To get the properties is not big deal, but I don´t want to get the properties inherited from another class. The bindingFlags option doesn´t have any option of this kind.

Is that possible ?

cheers

like image 474
2Fast4YouBR Avatar asked Aug 04 '10 16:08

2Fast4YouBR


1 Answers

Use BindingFlags.DeclaredOnly with your Type.GetProperties call in order to specify to just get the properties from the specified type.

For example, to get all non-static properties on a type without looking up it's hierarchy, you could do:

var properties = theType.GetProperties(
                          BindingFlags.Public | 
                          BindingFlags.NonPublic | 
                          BindingFlags.Instance | 
                          BindingFlags.DeclaredOnly);
like image 122
Reed Copsey Avatar answered Dec 02 '22 10:12

Reed Copsey