问题描述:

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.

相关阅读:
Top