Just as you can reflect, it's possible to have a C# program reflect upon itself.
For example, you can have a class reflect upon itself and this will tell you the methods or properties it contains. You'll find that being able to reflect on a program, a class, a type, or any other item enables you to take better advantage of it and its attributes.
The first step to this kind of 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, 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. 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), do the following:
MemberInfo[] MyMemberArray = MyTypeObject.GetMembers();
An array of
MemberInfo objects is created named
MyMemberArray. It is assigned the return value of the call to
GetMembers for the type stored in
MyTypeObject.
Now, the MyMemberArray contains the members of your type. You can loop through this array and evaluate each member. If you are completely confused, don't worry. The following listing pulls all this together. For fun, this listing reflects on a reflection-related class[md]the System.Reflection.PropertyInfo class.
The MemberInfo type is a part of the Reflection namespace. You need to include System.Reflection to use the shortened version of the name.
1: using System;
2: using System.Reflection;
3:
4: class MyMemberInfo
5: {
6: public static int Main()
7: {
8: //Get the Type and MemberInfo.
9: string testclass = "System.Reflection.PropertyInfo";
10:
11: Console.WriteLine ("\nFollowing is the member info for class:
{0}",
12: testclass);
13:
14: Type MyType = Type.GetType(testclass);
15:
16: MemberInfo[] MyMemberInfoArray = MyType.GetMembers();
17:
18: //Get the MemberType method and display the elements
19:
20: Console.WriteLine("\nThere are {0} members in {1}",
21: MyMemberInfoArray.GetLength(0),
22: MyType.FullName);
23:
24: for ( int counter = 0;
25: counter < MyMemberInfoArray.GetLength(0);
26: counter++ )
27: {
28: Console.WriteLine( "{0}. {1} Member type - {2}",
29: counter,
30: MyMemberInfoArray[counter].Name,
31: MyMemberInfoArray[counter].MemberType.ToString());
32: }
33: return 0;
34: }
35: }
Here's the output of this listing:
Following is the member info for class: System.Reflection.PropertyInfo
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
Bradley L. Jones