You want to examine specific type data by using a filter. | Technique The previous recipes used various Get Xxxx methods to return an array of methods , parameters, events, and properties. Each one of these cases used an empty parameter list. However, each method also contains another overloaded version, which accepts a number of flags. These flags allow you to filter the returned items based on certain criteria. The BindingFlags enumerated data type contains the FlagsAttribute , allowing you to use it as a bit field to specify several flags at once. Table 23.1 lists the valid values that you can use with reflection. As an example, to return all the methods of a Type that are private static methods, use the Static and NonPublic BindingFlags , as shown in the following code: foreach( MethodInfo method in someType.GetMethods( BindingFlags.Static BindingFlags.NonPublic ) ) { MessageBox.Show( method.Name ); } Table 23.1. BindingFlags Values Value | Effect | DeclaredOnly | Only returns members at current hierarchy level. Does not return inherited members . | Instance | Only returns instance members, excluding any static members. | Static | Specifies that static members should be returned. | Public | Returns members with public access permission. | NonPublic | Returns members that are marked as protected or private. | FlattenHierarchy | Returns any static members defined in inherited types. | Comments The assembly-viewer application up to this point displays a great deal of information about an assembly by enumerating each module, each module's type, and each type's individual members. Additionally, the members are each categorized based on the programmatic construct they define, which includes methods, events, and properties. However, if you compare the member nodes within the tree to the actual member declarations within a specific type, you notice that only public members appear. This behavior is the default behavior of all the Get Xxxx reflection methods. To include both public and nonpublic, you have to use the technique discussed in this recipe by utilizing BindingFlags . All that you need to perform reflection is a Type object. This chapter explicitly loaded an assembly using an OpenFileDialog , which was subsequently enumerated to extract type information. However, you can also use an instantiated object for reflection by calling the GetType method defined for every .NET object. Theoretically, you can call the GetType method of an object and perform a FindMembers call with a BindingFlags.NonPublic to retrieve all the private members within a Type . Furthermore, since the FieldInfo contains a method named GetValue , you'll also be able to get the current value of a private member variable for a Type defined in an assembly that some other developer created. Can you see the problem as it relates to security here? Fortunately, this type of field access is under tight control with .NET security. Without the property ReflectionPermission , you cannot do this move. Any attempt to do so without permission results in a thrown SecurityException . Code access security is discussed in Chapter 21, "Securing Code." |