Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection

If I use

sometype.GetProperties(); 

I get all of the properties from the type and it's parent. However I only want to retrieve the properties defined explicitly in this type (not the parents). I thought that was what the BindingFlags.DeclaredOnly option was for.

However, when I try this:

sometype.GetProperties(BindingFlags.DeclaredOnly); 

I get 0 properties.

Anyone know what I am doing wrong?

like image 437
UpTheCreek Avatar asked Oct 09 '09 17:10

UpTheCreek


People also ask

What is GetProperties C#?

GetProperties() Method Syntax: public System. Reflection. PropertyInfo[] GetProperties (); Return Value: This method returns an array of PropertyInfo objects representing all public properties of the current Type or an empty array of type PropertyInfo if the current Type does not have public properties.

What is BindingFlags C#?

BindingFlags values are used to control binding in methods in classes that find and invoke, create, get, and set members and types. To specify multiple BindingFlags values, use the bitwise 'OR' operator.

How do I get a property type from PropertyInfo?

You can do this by getting an array of all properties from the Type. GetProperties method and then iterating the elements in the array, or you can retrieve the PropertyInfo object that represents the property directly by calling the Type. GetProperty method and specifying the property name.

Which specifies option that control binding and the way in which the search for members and types is conducted by reflection?

Specifies flags that control binding and the way in which the search for members and types is conducted by reflection. This enumeration supports a bitwise combination of its member values.


2 Answers

If you specify any BindingFlags, then you need to specify explicitly what properties you want to get. For example:

sometype.GetProperties (BindingFlags.DeclaredOnly |                          BindingFlags.Public |                          BindingFlags.Instance); 
like image 116
Pete Avatar answered Oct 12 '22 19:10

Pete


To summarize:

  1. if you use the GetProperties() overload (without parameters), you will get all public properties.

  2. on the other hand, if you use the GetProperties(BindingFlags) overload (the one which accepts a BindingFlags parameter), then you need to specify a bitwise OR of at least one from each group of the following flags:

    • BindingFlags.Instance / BindingFlags.Static (instance vs static properties),
    • BindingFlags.Public / BindingFlags.NonPublic (public vs non-public properties).

For example, to get public static properties, you will need to call GetProperties(BindingFlags.Public | BindingFlags.Static) in order to get results. Calling only GetProperties(BindingFlags.Public) or GetProperties(BindingFlags.Static) won't return any results.

Note also that specifying BindingFlags.Default will return an empty array.

Full details can be found in MSDN documentation for GetProperties(BindingFlags):

The following BindingFlags filter flags can be used to define which nested types to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Public to include public properties in the search.
  • Specify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search. Only protected and internal methods on base classes are returned; private methods on base classes are not returned.
  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.
like image 41
Groo Avatar answered Oct 12 '22 18:10

Groo