How to list all members of a certain type

Source: Internet
Author: User
How to list all members of a certain type

This example allows you to list members of a given data type. The function of listing type members is a good way to quickly discover which elements are available. It is an important tool for reporting in the system and helping develop user documents. UseReflectionNamespace. You can control the member types and other information (such as the visibility of specific methods) that you want to display to users ). You can also obtain information about all the members in the class, or specify only some subsets (such as methods or fields ).

 

C # ListMembers. aspx

[Running example] | [View Source Code]

You may want to know why it is important to obtain specific types of information. After all, this is the purpose of the help system and help documentation, isn't it? The following example can help you create user documents, or help you call Methods dynamically or set attributes.

Perform the following steps. First, you need to obtain the type you want to use (in string form ). After determining the type to be used, you need to assign an object to indicate this type. This will take two steps: Create an object that can be used in the subsequent steps, and ensure that the specified type exists and can be found by the system. The following example allocates an object to the System. String type. Note: although the "Console" object in this example provides feedback to users, the actual example sends feedback to an ASP. NET tag object. But the explanation is the same.


// don't forget your using statements at the top of your code...Using System;Using System.Reflection;// class declaration, and method declaration...// remember that this string is case-sensitive, so be carefulType t = Type.GetType("System.String");// check to see if we have a valid value. If our object is null, the type does not exist...if (t == null) {// Don't assume that it is a SYSTEM datatype...Console.WriteLine("Please ensure you specify only valid types in the type field.");Console.WriteLine("REMEMBER: The Case matters (Byte is not the same as byte).");return; // don't continue processing}
'  don't forget your imports statements at the top of your code...Imports SystemImports System.Reflection'  class declaration, and method declaration...'  remember that this string is case-sensitive, so be carefulDim t As Type = Type.GetType("System.String")'  check to see if we have a valid value. If our object is null, the type does not exist...If t Is Nothing Then'  Don't assume that it is a SYSTEM datatype...Console.WriteLine("Please ensure you specify only valid types in the type field.")Console.WriteLine("REMEMBER: The Case matters (Byte is not the same as byte).")Exit Sub ' don't continue processingEnd If
C # VB  

After a valid type object is available, the next question is what type of Member is to be retrieved for the type? Is the method, static method, or instance field required? InReflectionThere is a group in the namespaceInfoObject. Each object represents a group of different members of your system. For example, there isMethodInfoAn object can indicate information about a method. There is also a generalMemberInfoObject, which indicates all the members that can exist in a given class.

With this information, you can set the following array to view the type you just created and find out the type information. In the following example, the "bit" Operator (| symbol, or BitOr in Visual Basic) requests all information that meets the specified constraints. This example shows how to obtain all fields and all methods.

// declare and populate the arrays to hold the information...FieldInfo [] fi = t.GetFields (BindingFlags.Static |BindingFlags.NonPublic | BindingFlags.Public);     // fieldsMethodInfo [] mi = t.GetMethods (BindingFlags.Static |BindingFlags.NonPublic | BindingFlags.Public);     // methods
'  declare and populate the arrays to hold the information...Dim fi() As FieldInfo = t.GetFields(BindingFlags.Static BitOr _BindingFlags.NonPublic BitOr BindingFlags.Public)  '  fieldsDim mi() As MethodInfo = t.GetMethods(BindingFlags.Static BitOr _BindingFlags.NonPublic BitOr BindingFlags.Public)  '  methods
C # VB  

The next step is to iterate through each array and list the elements in the array on the screen (obviously, you will actually process these elements or identify a specific element in the array ). You can perform this operation in multiple ways.Foreach(For Each in Visual Basic.

// iterate through all the method membersforeach (MethodInfo m in mi) {Console.WriteLine(m);}// iterate through all the field membersforeach (FieldInfo f in fi) {Console.WriteLine(f);}// etc.... for each array type
Dim m As MethodInfoDim f As FieldInfo'  iterate through all the method members...For Each m In miConsole.WriteLine(m)Next m'  iterate through all the field membersFor Each f In fiConsole.WriteLine(f)Next f'  etc.... for each array type
C # VB  

The previous code works well, but pay attention to the twoForeach(For Each in Visual Basic) the statement is similar. Writing all of this is very laborious and messy (and it may require a lot of maintenance if you change the code later ). You can returnMemberInfoObject To avoid this.MemberInfoObjects include all possible information sets (methods, fields, and interfaces ). This can help us, because we canForeachThe statement is written into an array for analysis.

// call the routine below, passing the relevant array we made in the previous stepPrintMembers( mi ); // the method informationPrintMembers( fi ); // the field informationvoid PrintMembers (MemberInfo [] ms ) {// MemberInfo is the generic info object. This can be any of the other info objects.foreach (MemberInfo m in ms) {Console.WriteLine(m);}}
'  call the routine below, passing the relevant array we made in the previous stepPrintMembers( mi )  '  the method informationPrintMembers( fi )  '  the field informationSub PrintMembers (ms() As MemberInfo)Dim m As MemberInfo'  MemberInfo is the generic info object. This can be any of the other info objects.For Each  m in msConsole.WriteLine(m)Next mEnd Sub
C # VB  

When running this example, you will notice that other things will happen at the same time. You do not need to hard encode the System. String object. You can specify the class to be retrieved. It also allows you to control whether to display static information or instance information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.