How can ZT obtain types in an assembly?

Source: Internet
Author: User
Tags mscorlib
How to obtain types in an assembly

This example shows how to retrieve all types of a given assembly. To browse the assembly type, you must first identify the Assembly you want to operate on. After an object references an assembly of interest, you can callGetTypesMethod, which returns an array containing all types in the Assembly. You can use the control logic to identify more specific types in the array, analyze your Array Using iteration logic, and return type information to the user as needed. The ability to retrieve type information is useful for identifying other types that can be used for a given task, or for identifying existing elements that can provide you with the required functionality.

 

C # GetTypes. aspx

[Running example] | [View Source Code]

When retrieving data from a specific assembly, you must first learn how to identify the assembly. This "Quick Start" shows two methods for retrieving an assembly. The first method is to identify the specific object to be searched in the Assembly and request the module of the object to the Assembly (remember that the module is a logical group of type and code, such. dll or. exe ). The second method is to useAssemblyClassLoadFromMethod to load a specific Assembly for a specified module (such as myapp.exe.


// don't forget your using statementsusing System;using System.Reflection;// ...// Getting an Assembly, method 1. Get the mscorlib assembly// Note that other types such as String, or Int32 would have worked just as well,// since they reside in the same assemblyAssembly a = typeof(Object).Module.Assembly;// Getting an Assembly, method 2. Load a particular assembly, using a reference to a// module that is within that assembly. Note that this requires a compiled module for// the reference, and when running in an aspx page, will require a fully qualifed path// to the file, to ensure it is correctly identifiedAssembly b = Assembly.LoadFrom ("GetTypes.exe");// note that either of the above methods is viable, depending on the information// you have. Since we know the name of the file which houses all of the base system// objects, we could do the following to replace the first example, just as effectively// (the absolute path may change on your machine)// Assembly a = Assembly.LoadFrom//("c:/winserv/microsoft.net/framework/v1.0.2230/mscorlib.dll");
'  don't forget your using statementsImports SystemImports System.Reflection' ...'  Getting an Assembly, method 1. Get the mscorlib assembly'  Note that other types such as String, or Int32 would have worked just as well,'  since they reside in the same assemblyDim a As reflection.Assembly = GetType(Object).Module.Assembly'  Getting an Assembly, method 2. Load a particular assembly, using a reference to a'  module that is within that assembly. Note that this requires a compiled module for'  the reference, and when running in an aspx page, will require a fully qualifed path'  to the file, to ensure it is correctly identifiedDim b As reflection.Assembly = reflection.Assembly.LoadFrom ("GetTypes.exe")'  note that either of the above methods is viable, depending on the information'  you have. Since we know the name of the file which houses all of the base system'  objects, we could do the following to replace the first example, just as effectively'  (the absolute path may change on your machine)'  Dim a As reflection.Assembly = reflection.Assembly.LoadFrom _'("c:/winserv/microsoft.net/framework/v1.0.2230/mscorlib.dll")
C # VB  

After the Assembly is identified, you can continue to retrieve the type.GetTypesThe Return Value of the method is assignedTypeObject array. Now you can operate on these types. In the following example, you obtain the type of the core Runtime Library and calculate the number of different types of styles in the Assembly (if you need) for more information about statements, see how to iterate through the content under the Set topic ). Although the number of other Members (such as classes and enumeration) can be calculated, this example only shows how to calculate the number of interfaces.

//Get all the types in the assembly identified in the previous exampleType [] types = a.GetTypes ();int numInterfaces = 0;foreach (Type t in types) {//the following line uses a set of methods which identify what//kind of type we are currently queryingif (t.IsInterface) {// only print out the names of the InterfacesConsole.WriteLine (t.Name + "");numInterfaces++;}}// write out the totalsConsole.WriteLine("Out of {0} types in the {1} library:",types.Length, typeof(Object).Module.ToString());Console.WriteLine ("{0} are interfaces (listed)", types.Length, numInterfaces);
' Get all the types in the assembly identified in the previous exampleDim types() As Type = a.GetTypes ()Dim numInterfaces As Integer = 0Dim t As TypeFor Each t in types' the following line uses a set of methods which identify what' kind of type we are currently queryingIf t.IsInterface Then'  only print out the names of the InterfacesConsole.WriteLine (t.Name + "'")numInterfaces = numInterfaces + 1End IfNext t'  write out the totalsConsole.WriteLine("Out of {0} types in the {1} library:", _types.Length, GetType(Object).Module.ToString())Console.WriteLine ("{0} are interfaces (listed)", types.Length, numInterfaces)
C # VB  

You can also use the second method identified in the first example to retrieve the type of the given assembly. In the following example, you will notice that it does not use the same base structure to pass the type in sequence, because you do not need to trace different types. This is suitable for viewing small datasets, such as the currently running applications (retrieving a list of all types of MSCorLib produces a very large list ).

// Get all the types in the assembly identified in the previous example (this assembly)Type [] types2 = b.GetTypes ();Console.WriteLine ("Get all the types from the assembly: '{0}'", b.GetName());foreach (Type t in types2){    Console.WriteLine (t.FullName);}
'  Get all the types in the assembly identified in the previous example (this assembly)Dim types2() As Type = b.GetTypes ()Console.WriteLine ("Get all the types from the assembly: '{0}'", b.GetName())For Each t in types2Console.WriteLine(t.FullName) ' not many types, so we can print them allNext t
C # VB  

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.