72

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?

SteveC
  • 15,808
  • 23
  • 102
  • 173
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
  • 25
    `To all who come from google:` if you still can't get your "properties" even after setting flags, if you're new to c# and don't know the difference between `Properties` and `Fields`, try using `GetFields()`. You've probably been looking for `fields` all along! – user1306322 Aug 30 '12 at 06:36
  • 1
    @user1306322: just for the record, I "come from google" and my code is: `var types = type.GetFields.Select(f => f.FieldType).Union(type.GetProperties.Select(p => p.PropertyType));` - and the results still count 0. – Veverke Sep 10 '15 at 15:10

5 Answers5

94

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);
SteveC
  • 15,808
  • 23
  • 102
  • 173
Pete
  • 11,313
  • 4
  • 43
  • 54
55

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.
Community
  • 1
  • 1
vgru
  • 49,838
  • 16
  • 120
  • 201
  • So to point this out: *without* any BindingFlags, there are some defaults. Do you know which they are? – Stefan Steinegger Oct 09 '09 at 17:07
  • 1
    If you use the `GetProperties()` overload (without parameters), you will get all public properties. But `GetProperties(BindingFlags.Default)` will return an empty array. – vgru Oct 09 '09 at 17:20
  • Heh, so what's the use of BindingFlags.Default?! ;) – UpTheCreek Oct 09 '09 at 17:22
  • It's recommended that all enums marked with a Flags attribute have a defined enum value for `0`. Since the underlying type for the enums flags is int, you must have an enum which represents the default value of int. You are likely to get a 0 value when you are instantiating it or doing bitwise operations with the flags. – vgru Oct 09 '09 at 17:51
  • "then you need to specify a logical OR" - you mean a bitwise OR ? – rom99 Feb 24 '15 at 17:27
  • 1
    ➕1 for including the details of that arcane enum – Chris Marisic May 04 '16 at 14:13
21

You need to expand your BindingsFlag a bit. They need to at least include what accessibility level and instance vs. static in order to get anything meaningful back.

I think what you are actually looking for is the following

var flags = BindingFlags.DeclaredOnly 
  | BindingFlags.Instance
  | BindingFlags.Public;
someType.GetProperties(flags);
Pasi Savolainen
  • 2,460
  • 1
  • 22
  • 35
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

From the MSDN site.

Default (member) Specifies no binding flag.

You must specify Instance or Static along with Public or NonPublic or no members will be returned.

Hence unless you specify the binding flags you get nothing.

Community
  • 1
  • 1
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
-1

I had problems using typeof(Thing), eventually this worked for me:

        var thisThing = (new Thing()).GetType();
        foreach (var property in thisThing.GetProperties())
        {
            // ...
        }
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183