devxlogo

Use C#’s Reflection to Retrieve Application Information

Use C#’s Reflection to Retrieve Application Information

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.

See also  Why ChatGPT Is So Important Today

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 ("
Following 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 elements19:20:        Console.WriteLine("
There 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.PropertyInfoThere are 36 members in System.Reflection.PropertyInfo0. get_CanWrite Member type - Method1. get_CanRead Member type - Method2. get_Attributes Member type - Method3. GetIndexParameters Member type - Method4. GetSetMethod Member type - Method5. GetGetMethod Member type - Method6. GetAccessors Member type - Method7. SetValue Member type - Method8. SetValue Member type - Method9. GetValue Member type - Method10. GetValue Member type - Method11. get_PropertyType Member type - Method12. IsDefined Member type - Method13. GetCustomAttributes Member type - Method14. GetCustomAttributes Member type - Method15. get_ReflectedType Member type - Method16. get_DeclaringType Member type - Method17. get_Name Member type - Method18. get_MemberType Member type - Method19. GetHashCode Member type - Method20. Equals Member type - Method21. ToString Member type - Method22. GetAccessors Member type - Method23. GetGetMethod Member type - Method24. GetSetMethod Member type - Method25. get_IsSpecialName Member type - Method26. GetType Member type - Method27. MemberType Member type - Property28. PropertyType Member type - Property29. Attributes Member type - Property30. IsSpecialName Member type - Property31. CanRead Member type - Property32. CanWrite Member type - Property33. Name Member type - Property34. DeclaringType Member type - Property35. ReflectedType Member type - Property
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist