Reflect on Life; Reflect on C#

toraj58 0 Tallied Votes 145 Views Share

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

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 for reflection is to get the type of a type. You get the type of a class (or
other type) using the static method Type.GetType.

The return value of this method is a type that can be assigned to a Type object. The GetType method uses virtually any data type as a parameter. For example, to get the type of a class named TestClass and assign it to a Type object named MyTestObject, 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

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 ***

There are 36 members in System.Reflection.PropertyInfo
0. get_CanWrite Member type - Method
1. get_CanRead Member type - Method
2. get_Attributes Member type - Method
3. GetIndexParameters Member type - Method
4. GetSetMethod Member type - Method
5. GetGetMethod Member type - Method
6. GetAccessors Member type - Method
7. SetValue Member type - Method
8. SetValue Member type - Method
9. GetValue Member type - Method
10. GetValue Member type - Method
11. get_PropertyType Member type - Method
12. IsDefined Member type - Method
13. GetCustomAttributes Member type - Method
14. GetCustomAttributes Member type - Method
15. get_ReflectedType Member type - Method
16. get_DeclaringType Member type - Method
17. get_Name Member type - Method
18. get_MemberType Member type - Method
19. GetHashCode Member type - Method
20. Equals Member type - Method
21. ToString Member type - Method
22. GetAccessors Member type - Method
23. GetGetMethod Member type - Method
24. GetSetMethod Member type - Method
25. get_IsSpecialName Member type - Method
26. GetType Member type - Method
27. MemberType Member type - Property
28. PropertyType Member type - Property
29. Attributes Member type - Property
30. IsSpecialName Member type - Property
31. CanRead Member type - Property
32. CanWrite Member type - Property
33. Name Member type - Property
34. DeclaringType Member type - Property
35. ReflectedType Member type - Property

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

By: Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]

using System;
  using System.Reflection;
  class MyMemberInfo
     public static int Main()
        //Get the Type and MemberInfo.
        string testclass = "System.Reflection.PropertyInfo"; 
        Console.WriteLine ("\nFollowing is the member info for class: {0}",
        Type MyType = Type.GetType(testclass);
        MemberInfo[] MyMemberInfoArray = MyType.GetMembers();
        //Get the MemberType method and display the elements
        Console.WriteLine("\nThere are {0} members in {1}",
        for ( int counter = 0;
              counter < MyMemberInfoArray.GetLength(0);
              counter++ )
           Console.WriteLine( "{0}. {1} Member type - {2}",
        return 0;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.