简体   繁体   English

为什么我需要指定多种类型的绑定标志来获取单个字段的信息?

[英]Why do I need to specify multiple types of binding flag to get info on a single field?

When trying to get field information using reflection, I need to use the code 当尝试使用反射获取字段信息时,我需要使用代码

SomeObject.GetType().GetField(
    "FieldName",
    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)

In particular, I must specify both BindingFlags.NonPublic and BindingFlags.Instance . 特别是,我必须同时指定BindingFlags.NonPublic BindingFlags.Instance If I only specify one, I get a null return. 如果我只指定一个,我得到一个null返回。

If I am only looking at a single field, why do I need to specify multiple binding flag types? 如果我只查看单个字段,为什么需要指定多个绑定标志类型?

These flags play a role of some sort of a filter. 这些标志起着某种过滤器的作用。

If you omit BindingFlags.NonPublic , GetField() function does not look for private , internal and protected fields. 如果省略BindingFlags.NonPublic ,则GetField()函数不会查找privateinternalprotected字段。

And you have to specify either BindingFlags.Instance or BindingFlags.Static to define what you are looking for. 您必须指定BindingFlags.InstanceBindingFlags.Static来定义您要查找的内容。

See http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx : 请参阅http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx

You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return. 您必须指定BindingFlags.InstanceBindingFlags.Static才能获得返回。

... ...

Specify BindingFlags.NonPublic to include non-public fields (that is, private, internal, and protected fields) in the search. 指定BindingFlags.NonPublic以在搜索中包含非公共字段(即私有,内部和受保护字段)。 Only protected and internal fields on base classes are returned; 仅返回基类上的受保护和内部字段; private fields on base classes are not returned. 不返回基类上的私有字段。

It's how the search is implemented. 这就是搜索的实现方式。 From MSDN (in the Note section): MSDN (在Note部分):

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

every BindingFlag has a meaning 每个BindingFlag都有意义

flags do not specify how many fields you'll receive but what kind of field GetField method should look for flags不指定您将收到多少字段,但GetField方法应该查找哪种字段

for example if you do not specify NonPublic then you may not be able to retrieve any private, protected or internal fields 例如,如果您未指定NonPublic则可能无法检索任何私有,受保护或内部字段

in your case 在你的情况下

  • NonPublic - your desired property is not public NonPublic - 您想要的财产不公开
  • Instance - your desired field is an instance field not a static field 实例 - 您想要的字段是实例字段而不是静态字段

other common flags 其他常见的旗帜

  • DeclaredOnly - which are declared in the class, not inherited one DeclaredOnly - 在类中声明,不是继承的
  • FlattenHierarchy - it will look into every base class for the given name FlattenHierarchy - 它将查看给定名称的每个基类
  • IgnoreCase - as implies ignored the casing of the name IgnoreCase - 暗示忽略了名称的大小写
  • Public - looks for public members 公共 - 寻找公共成员
  • Static - looks for static members 静态 - 查找静态成员

more on BindingFlags 更多关于BindingFlags

The flags form a filter. 标志形成一个过滤器。 They define the types of fields returned. 它们定义了返回的字段类型。 If your field is not public and is an instance field (ie is not a static field), you need to include these flags. 如果您的字段不是公共字段且是实例字段(即不是静态字段),则需要包含这些标记。

You could as well specify additional flags like BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static 您还可以指定其他标志,如BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static in order to include other filed types. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static以包含其他字段类型。

if it is going about: System.Reflection.BindingFlags.Instance - from msdn: 如果是这样的话: System.Reflection.BindingFlags.Instance - 来自msdn:

You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return. 您必须指定BindingFlags.Instance或BindingFlags.Static才能获得返回。

(source) (资源)

That flag is required to determine wheter you want to get Static or Non-Static members (or both). 需要该标志来确定您希望获得静态或非静态成员(或两者)。

Next System.Reflection.BindingFlags.NonPublic tells, that you want to get non-public member (that by default are not visible outside class). Next System.Reflection.BindingFlags.NonPublic告诉您,您希望获得非公共成员(默认情况下,在类外部不可见)。 Fields are usually private that is why probably you get null when trying to retreive field without BindingFlags.Public . 字段通常是私有的,这就是为什么在没有BindingFlags.Public尝试retreive字段时可能会得到null

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM