Sometimes it is good to sit back and reflect on life. More specifically, you can sit back
and reflect on yourself. Often you will discover information that you didn’t realize about
yourself.
it is possible to have a C# program reflect upon itself.
You can use such reflection to learn about an application. For example, you can have a class reflect upon itself and tell you the methods or properties it contains. You’ll find that being able to reflect on a program, a class, a type, or another item will enable you to take more
advantage of it.
The key to getting information on a type (remember, a class is a type) is to use a reflec-
tion method. For example, the GetMembers method can be used to get a type’s members.
You get the list of members by passing GetMembers a Type type. Yes, Type is a type that
holds types. Read that sentence slowly, and it should make sense.
The first step, you do the following:
Type MyTypeObject = Type.GetType(TestClass);
MyTypeObject then contains the type for TestClass. You can use MyTypeObject to get the
members of a TestClass. As stated, this is done using the GetMembers method. The
GetMembers method returns an array of MemberItems. To call the GetMember method on the MyTypeObject (which contains the type of a TestClass in this example), you do the
following:
MemberInfo[] MyMemberArray = MyTypeObject.GetMembers();
An array of MemberInfo objects named MyMemberArray is created, which is assigned the
return value of the call to GetMembers for the type stored in MyTypeObject.
After you’ve done this assignment, the MyMemberArray contains the members of your type.
You can loop through this array and evaluate each member.
See the code snippet for an example and the output of the code is:
*** output ***
Before digging into the code, take a look at the output. You can see that there are 36
members in the System.Reflection.PropertyInfo class. In the line numbered 0 of the out-
put, the first member is the get_CanWrite member, which is a method. Other members are
get_CanRead, get_Attributes, GetIndexParameters, and so forth. Look at the lines numbered
13 and 14 of the output. They appear to be the same—both contain GetCustomAttributes.
Is this an error? No! Each overloaded method is a separate member, as it should be.
key point—reflection can happen at runtime.
*** ANALYSIS ***
The first thing to note about the code is that the System.Reflection namespace is
included in Line 2. This is necessary for the reflection members that will be used
in the listing.
In Line 9, a specific class name is assigned to a variable. This makes it easy for you to
reflect on different classes—just change the name stored in this string.
In Line 14, the name of a class is passed to the Type.GetType method. The returned type is
assigned to the variable MyType. The MyType object is then used to get the members of the
type it contains. These are assigned to a MemberInfo array named MyMemberInfoArray in
Line 16. Lines 24–32 then loop through this array and print the Name and the MemberType
values for each element. As you can see, the Name element contains the name of the
member. The MemberType when displayed as a string tells you the type of the individual
member.
By: Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]