8.3. Modifying the Retrieval through BindingFlags

As it happens, the string class has five properties, only two of which are public. By default, the Get methods retrieve only public instance members—both inherited members and those declared in the class. We can use a BindingFlags enum object to override the default retrieval policy to retrieve both static and nonpublic members or to suppress retrieval of inherited members.

The BindingFlags enumerators provide instructions to the retrieval algorithm as to what members to consider. The default pair of BindingFlags enumerators consists of Public and Instance. If we want to retrieve the nonpublic members as well, we pass in the NonPublic enumerator.[1] The following, however, does not work:

[1] Provided ...

Get C# Primer: A Practical Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.