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

后端 未结 5 1796
天涯浪人
天涯浪人 2020-12-01 07:02

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 proper

相关标签:
5条回答
  • 2020-12-01 07:35

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

            var thisThing = (new Thing()).GetType();
            foreach (var property in thisThing.GetProperties())
            {
                // ...
            }
    
    0 讨论(0)
  • 2020-12-01 07:37

    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.
    0 讨论(0)
  • 2020-12-01 07:39

    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);
    
    0 讨论(0)
  • 2020-12-01 07:55

    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);
    
    0 讨论(0)
  • 2020-12-01 07:57

    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.

    0 讨论(0)
提交回复
热议问题