Working with members is slightly different than working with methods. The reason for this is that there are many different member types, including properties, fields, methods, constructors, and so on. When you obtain a MemberInfo instance, you can take a look at the MemberType property to determine which kind of member you're looking at.
Table 11.3 gives you a quick overview of some of the more common properties and methods of the MemberInfo class.
When dealing with members of a specific type, you can obtain more detailed information about that member using the appropriate class. For example, you can get more detailed information about a property from the PropertyInfo class. Similar classes are available, such as ConstructorInfo, MethodInfo, FieldInfo, EventInfo, and ParameterInfo. Each of these classes has a corresponding Getxxxx method on the containing type. For instance, to obtain a specific field, you would use the GetField method on the type itself, which returns a FieldInfo instance.
In some cases, when you attempt to retrieve a member, you can pass a BindingFlags value. This value is a bitwise-OR'd list of scope items that tell reflection what types of items you want to search for when attempting to locate a member.
Table 11.4 shows a brief summary of some of the different values that you can use for the BindingFlags enumeration when locating type members. This isn't the entire list. For the entire list, you can look up the BindingFlags enumeration in the online MSDN documentation.
Listing 11.2 shows an example of accessing member information through Reflection. Note that for simplicity, the Customer class from the preceding sample was reused.
Listing 11.2. Reflecting on Member Information
The keen (or suspicious) eye may have already noticed that the preceding code can actually read data from a private member, even on a class that is completely unrelated to the one making the Reflection calls. This is always a concern, and as such you should never assume that even your in-memory data is secure. You will see some ways in which you can protect your data in-memory in Chapter 15, "Cryptography and Data Protection."
The output from the preceding sample produces text that looks like the following:
DoSomething (Method) get_PubData (Method) set_PubData (Method) GetType (Method) MemberwiseClone (Method) ToString (Method) Equals (Method) GetHashCode (Method) Finalize (Method) .ctor (Constructor) PubData (Property) Property Data Type : System.Int32 Current Value: 12 privData (Field) Field Data Type: System.Int32 Current Value: 12