Working with Member Information

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.

Table 11.3. MemberInfo Properties and Methods




Indicates the type of the member. Can be: Constructor, Custom, Event, Field, Method, NestedType, Property, or TypeInfo.


Name of the member.


Module in which the member was defined.


Gets a list of custom attributes that belong to the member. Attributes are discussed later in the section "Creating and Examining Custom Code Attributes."

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.

Table 11.4. BindingFlags Enumeration Members




Indicates that the case of the member name should be ignored when locating.


Indicates that instance members are to be included in the member search.


Indicates that nonpublic members should be included in the search. Without this flag, nonpublic members will not be located.


Indicates that public members should be included in the search.


Indicates that static members should be included in the search.

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

using System; using System.Reflection; using System.Collections.Generic; using System.Text; namespace ReflectMembers { class Program { static void Main(string[] args) { ReflectMethods.Customer<string> cust = new ReflectMethods.Customer<string>(); Type t = cust.GetType(); MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Public |     BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance); foreach (MemberInfo member in members) {     Console.WriteLine("{0} ({1})", member.Name, member.MemberType.ToString());     if (member.MemberType == MemberTypes.Property)     {         Console.WriteLine("\tProperty");         PropertyInfo pi = t.GetProperty(member.Name);         Console.WriteLine("\t\tData Type : {0}", pi.PropertyType.ToString());         Console.WriteLine("\t\tCurrent Value: {0}", pi.GetValue(cust, null));     }     if (member.MemberType == MemberTypes.Field)     {         Console.WriteLine("\tField");         FieldInfo fi = t.GetField(member.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);         Console.WriteLine("\t\tData Type: {0}", fi.FieldType.ToString());         Console.WriteLine("\t\tCurrent Value: {0}", fi.GetValue(cust));    } } Console.ReadLine(); } } } 

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 

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: