UseAssemblyClass can load the Assembly, browse the metadata and components of the assembly, discover types contained in the Assembly, and create instances of these types.
To obtain an array of Assembly objects (indicating the Assembly currently loaded into the application domain (for example, the default application domain of a simple project), you can use appdomain .. ::. getassemblies method.
To dynamically load an assembly, the Assembly class provides the following static methods (shared in Visual Basic ). The Assembly is loaded to the application domain in which the load operation occurs.
The recommended method for loading an assembly is to useLoadMethod, which identifies the assembly to be loaded by its display name (for example, "system. Windows. Forms, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089 "). When searching for a dataset, follow the rules described in the Assembly during the runtime.
You can use the reflectiononlyload and reflectiononlyloadfrom methods to load the Assembly for reflection, but not for execution. For example, you can check the 64-bit platform assembly by running code on the 32-bit platform.
The LoadFile and loadfrom methods are provided for a very small number of solutions that must be identified by the path assembly.
To obtain the Assembly object of the currently executed assembly, you can use the getexecutingassembly method.
Many members of the Assembly class provide information about the assembly. For example:
- The getname method returns an assemblyname object that provides access to the display name of the Assembly.
- The getcustomattributes method lists attributes applied to an assembly.
- The getfiles method provides access to files in the assembly list.
- The getmanifestresourcenames method provides the name of the resource in the assembly list.
- The gettypes method lists all types in the Assembly. The getexportedtypes method lists types visible to callers other than the Assembly. The GetType method can be used to search for a specific type in a dataset. The createinstance method can be used to search for and create instances of the type in the Assembly.
1 using system; 2 using system. reflection; 3 4 namespace assemblytest 5 {6 class program 7 {8 Static void main (string [] ARGs) 9 {10 try11 {12 showallloadassemblies (); 13 loadassignassembly (); 14} 15 catch (exception ex) 16 {17 console. writeline (ex. message); 18} 19 console. readkey (); 20} 21 // <summary> 22 // obtain the list of loaded assemblies in the current program domain 23 // </Summary> 24 public static void showallloadassemblies () 25 {26 assem Bly [] assemblies = appdomain. currentdomain. getassemblies (); 27 console. writeline ("the following assembly has been loaded in the current program domain:"); 28 foreach (Assembly in assemblies) 29 {30 console. writeline (assembly); 31} 32} 33 // <summary> 34 // load the specified Assembly 35 /// </Summary> 36 public static void loadassignassembly () 37 {38 console. writeline ("enter a collection name:"); 39 string assembiyname = console. readline (); 40 // The loadfrom method is for a very small number of scenarios where the Assembly must be identified by the path 41 Assembly A = assembly. loadfrom (assembiyname); 42 console. writeline ("=================================\ N after loading the Assembly {0 \ N =================================================== = ", a. fullname); 43 // refresh to display the currently loaded Assembly 44 showallloadassemblies (); 45 console. writeline ("===============================================\ n information about the newly loaded assembly \ n ================================= "); 46 console. writeline ("assembly Full name:" +. fullname); 47 # region assemblyname annotation 48 // The getname method returns an assemblyna Me object, which provides access to the Assembly display name section. 49 // The assemblyname object contains information about the assembly, which can be used when bound to the Assembly. The Assembly identifier consists of the following parts: 50 // simple name. 51 // version number. 52 // encrypt the key pair. 53 // supported regions. 54 # endregion55 console. writeline ("assembly version" +. getname (). version); 56 console. writeline ("initial position of the Assembly:" +. codebase); 57 console. writeline ("assembly location:" +. location); 58 console. writeline ("assembly entry point:" +. entrypoint); 59 console. writeline ("whether it is in the Global Assembly Cache:" +. globalassemblycache); 60 console. writeline ("RunTime version of public language:" +. imageruntimeversion); 61 62 // obtain the assembly type 63 type [] types =. gettypes (); 64 console. writeline ("\ n this Assembly contains the following types:"); 65 foreach (type in types) 66 {67 console. writeline ("type name:" + type. name); 68} 69 70 // get Assembly reference assembly information 71 assemblyname [] assemblynames =. getreferencedassemblies (); 72 console. writeline ("all the Assembly referenced by this Assembly is as follows:"); 73 foreach (assemblyname ass in assemblynames) 74 {75 console. writeline (ASS); 76} 77} 78} 79}