简体   繁体   中英

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 ?

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?

Your table is not 100% correct.

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. So if you use the NonPublic filter, then you will get private, protected, internal and protected internal methods.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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