Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Reflection.BindingFlags.Instance correspondence to C# access modifers

Tags:

c#

.net

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?

like image 493
JDiMatteo Avatar asked Dec 19 '14 17:12

JDiMatteo


People also ask

What is BindingFlags instance in C#?

This binding flag is used for methods with parameters that have default values and methods with variable arguments (varargs). This flag should only be used with InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).

What is BindingFlags?

BindingFlags values are used to control binding in methods in classes that find and invoke, create, get, and set members and types.


1 Answers

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.

like image 98
msporek Avatar answered Nov 13 '22 23:11

msporek