简体   繁体   English

在.NET Reflection中使用GetProperties()和BindingFlags.DeclaredOnly

[英]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. 我认为这就是BindingFlags.DeclaredOnly选项的用途。

However, when I try this: 但是,当我尝试这个:

sometype.GetProperties(BindingFlags.DeclaredOnly);

I get 0 properties. 我得到0个属性。

Anyone know what I am doing wrong? 谁知道我做错了什么?

If you specify any BindingFlags , then you need to specify explicitly what properties you want to get. 如果指定任何BindingFlags ,则需要明确指定要获取的属性。 For example: 例如:

sometype.GetProperties (BindingFlags.DeclaredOnly | 
                        BindingFlags.Public | 
                        BindingFlags.Instance);

To summarize: 总结一下:

  1. if you use the GetProperties() overload (without parameters), you will get all public properties . 如果您使用GetProperties()重载(不带参数),您将获得所有公共属性

  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: 另一方面,如果使用GetProperties(BindingFlags)重载(接受BindingFlags参数的重载),则需要在以下标志的每组中指定至少一个的按位OR:

    • BindingFlags.Instance / BindingFlags.Static (instance vs static properties), BindingFlags.Instance / BindingFlags.Static (实例与静态属性),
    • BindingFlags.Public / BindingFlags.NonPublic (public vs non-public properties). BindingFlags.Public / BindingFlags.NonPublic (公共与非公共属性)。

For example, to get public static properties, you will need to call GetProperties(BindingFlags.Public | BindingFlags.Static) in order to get results. 例如,要获取公共静态属性,您需要调用GetProperties(BindingFlags.Public | BindingFlags.Static)才能获得结果。 Calling only GetProperties(BindingFlags.Public) or GetProperties(BindingFlags.Static) won't return any results. 仅调用GetProperties(BindingFlags.Public)GetProperties(BindingFlags.Static)将不会返回任何结果。

Note also that specifying BindingFlags.Default will return an empty array . 另请注意,指定BindingFlags.Default将返回一个空数组

Full details can be found in MSDN documentation for GetProperties(BindingFlags) : 有关详细信息,请参阅GetProperties(BindingFlags)的MSDN文档

The following BindingFlags filter flags can be used to define which nested types to include in the search: 以下BindingFlags 过滤器标志可用于定义要包含在搜索中的嵌套类型:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return. 您必须指定BindingFlags.InstanceBindingFlags.Static才能获得返回。
  • Specify BindingFlags.Public to include public properties in the search. 指定BindingFlags.Public以在搜索中包含公共属性。
  • Specify BindingFlags.NonPublic to include non-public methods (that is, private, internal, and protected methods) in the search. 指定BindingFlags.NonPublic以在搜索中包含非公共方法(即私有,内部和受保护方法)。 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; 指定BindingFlags.FlattenHierarchy以在层次结构中包含公共和受保护的静态成员; private static members in inherited classes are not included. 不包括继承类中的私有静态成员。

The following BindingFlags modifier flags can be used to change how the search works: 以下BindingFlags 修饰符标志可用于更改搜索的工作方式:

  • BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited. BindingFlags.DeclaredOnly仅搜索在Type上声明的属性,而不是仅继承的属性。

You need to expand your BindingsFlag a bit. 你需要稍微扩展你的BindingsFlag。 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);

From the MSDN site. 来自MSDN网站。

Default (member) Specifies no binding flag. 默认(成员)指定无绑定标志。

You must specify Instance or Static along with Public or NonPublic or no members will be returned. 您必须与Public或NonPublic一起指定Instance或Static,否则将不返回任何成员。

Hence unless you specify the binding flags you get nothing. 因此,除非您指定绑定标志,否则什么也得不到。

I had problems using typeof(Thing) , eventually this worked for me: 我在使用typeof(Thing)遇到了问题,最终这对我有用:

        var thisThing = (new Thing()).GetType();
        foreach (var property in thisThing.GetProperties())
        {
            // ...
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BindingFlags.DeclaredOnly替代方法,以避免派生类的不明确属性(AmbiguousMatchException) - BindingFlags.DeclaredOnly alternative to avoid ambiguous properties of derived classes (AmbiguousMatchException) .Net反射GetProperties() - .Net Reflection GetProperties() C#反射方法GetProperties(BindingFlags.Instance)不返回子类对象 - C# Reflection method GetProperties(BindingFlags.Instance) not returning child class objects 使用反射(Type.GetProperties)获取DependencyProperties? - Get DependencyProperties using reflection (Type.GetProperties)? PCL Reflection使用BindingFlags获取属性 - PCL Reflection get properties with BindingFlags 具有适当的BindingFlags的继承/子类的GetProperties不返回预期的属性 - GetProperties for inherited/child class with appropriate BindingFlags does not return expected properties 带有 BindingFlags.Public 的方法 GetProperties 不返回任何内容 - Method GetProperties with BindingFlags.Public doesn't return anything 没有使用反射的实例化对象的GetProperties列表中的GetValue - GetValue from list of GetProperties without an instantiated object using reflection C#反射帮助? (的GetProperties) - C# Reflection Help? (GetProperties) 在不同的自定义类和集合上的反射GetProperty - Reflection GetProperties on different custom classes and collections
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM