简体   繁体   English

System.Reflection.BindingFlags.Instance与C#访问修改器的对应关系

[英]System.Reflection.BindingFlags.Instance correspondence to C# access modifers

How do the System.Reflection.BindingFlags Public, NonPublic, and Instance correspond to the C# access modifiers ? System.Reflection.BindingFlags Public,NonPublic和Instance如何与C# 访问修饰符相对应?

Is the following correspondence table correct? 以下对应表是否正确?

+-------------+--------+---------+-----------+----------+--------------------+
| BindingFlag | Public | Private | Protected | Internal | Protected Internal |
+-------------+--------+---------+-----------+----------+--------------------+
| Instance    | No     | No      | No        | Yes      | Yes                |
| NonPublic   | No     | Yes     | Yes       | No       | No                 |
| Public      | Yes    | No      | No        | No       | No                 |
| *           | Yes    | Yes     | Yes       | Yes      | Yes                |
+-------------+--------+---------+-----------+----------+--------------------+

* Instance | NonPublic | Public

Is there a way to make sense of this? 有没有办法理解这个? For example, if Instance corresponds to Internal, why isn't it just called Internal? 例如,如果Instance对应于Internal,为什么它不仅仅称为Internal?

Your table is not 100% correct. 您的表格不是100%正确。

Instance means that this is an "instance method" which means non-static. 实例意味着这是一个“实例方法”,这意味着非静态。 If you want to get non-static methods, then you use the Instance filter. 如果要获取非静态方法,则使用“实例”过滤器。 If you want to get static methods then you cannot put this filter. 如果你想获得静态方法,那么你就不能把这个过滤器。

NonPublic means anything except for public methods. NonPublic表示除公共方法之外的任何内容。 So if you use the NonPublic filter, then you will get private, protected, internal and protected internal methods. 因此,如果您使用NonPublic过滤器,那么您将获得私有,受保护,内部和受保护的内部方法。

Public means just public methods, and no other methods. 公共意味着公共方法,没有其他方法。

Your table should look like that: 你的表应该是这样的:

+-------------+--------+---------+-----------+----------+--------------------+
| BindingFlag | Public | Private | Protected | Internal | Protected Internal |
+-------------+--------+---------+-----------+----------+--------------------+
| NonPublic   | No     | Yes     | Yes       | Yes      | Yes                |
| Public      | Yes    | No      | No        | No       | No                 |
+-------------+--------+---------+-----------+----------+--------------------+

Putting "Instance" filter in this table doesn't make sense, as Instance does not deal with method's access level. 将“Instance”过滤器放在此表中是没有意义的,因为Instance不处理方法的访问级别。

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

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