Dynamically loadable assemblies. Class Assembly. Methods Load() and LoadFrom()
Contents
- 1. How is it possible to connect the assembly in the program?
- 2. What is the essence of the dynamic loading of assembly in the program?
- 3. The purpose of the class System.Assembly. Methods Load() and LoadFrom()
- 4. An example of dynamic loading and use of an external private assembly. Method Load()
- Related topics
Search other websites:
1. How is it possible to connect the assembly in the program?
An external assembly can be connected to the program in two ways:
- in static way by using special tools from Microsoft Visual Studio. This is done, for example, with the commands “Add Reference …” or “Add Service Reference …”. In this case, the assembly manifest contains the relevant information about the external assembly that was connected;
- dynamically using the Assembly class which is located in the System.Reflection namespace. In this case, the information about the external assembly is not placed in the manifest of the current assembly. This information is obtained on the fly, that is, dynamically. There are a number of tasks in which information about an assembly in a program should be obtained dynamically.
⇑
2. What is the essence of the dynamic loading of assembly in the program?
A program that is hosted in an assembly can include other assemblies in order to use their capabilities (classes, interfaces, methods, etc.). Dynamic loading of assembly is the process of loading and retrieving information about external assemblies on demand during program execution. When dynamically loading an external assembly, there is no information in the manifest about this assembly. The information is obtained programmatically.
⇑
3. The purpose of the class System.Assembly. Methods Load() and LoadFrom()
The System.Assembly class contains tools for dynamically loading assemblies and viewing their properties. The System.Assembly class is located in the System.Reflection namespace. In order to use the tools of the System.Assembly class, you need to type the following code at the beginning of the program module
using System.Reflection;
To load an assembly, you need to invoke one of the methods:
- method Load() for private assemblies. Private assemblies are located in the same directory as the program that uses them (explores);
- method LoadFrom() for shared assemblies. Shared assemblies are libraries that can be used by different applications on the same machine. Shared assemblies are deployed in a special GAC (Global Assembly Cache) directory.
⇑
4. An example of dynamic loading and use of an external private assembly. Method Load()
The example covers some operations that define information about mscorlib.dll assembly. Using this sample, you can get information about your own developed assemblies, which are saved in *.dll files.
The following is the text of the GetAssemblyInfo() method, which:
- uses the Assembly.Load() method to create an object that contains information about the assembly mscorlib.dll;
- for demonstration purposes, it receives and displays all types that are in the assembly using the GetTypes() method;
- for demonstration purposes, it receives information about a specific System.Math class, which is implemented in the mscorlib.dll assembly. Displays the names of the methods in the System.Math class.
// method that demonstrates getting information about mscorlib.dll assembly static void GetAssemblyInfo() { // get a list of names of types of assembly mscorlib.dll Assembly asm; asm = Assembly.Load("mscorlib.dll"); // instead of this, you can specify your own assembly // get general information about the assembly string FullName = asm.FullName; // Full Name = mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 // get a list of names in the assembly Type[] types = asm.GetTypes(); // get all the types that are in the assembly // display the names of all types of assembly on the screen foreach (Type t in types) { Console.WriteLine("Type: {0}", t.FullName); } // get information about a specific type Type tp = asm.GetType("System.Math"); // the System.Math type MethodInfo[] methods = tp.GetMethods(); // get information about System.Math methods // display a list of System.Math methods foreach (MethodInfo mi in methods) { Console.WriteLine(mi.Name); // display only method names } }
More detailed examples of software code for determining the necessary information of a given type are described in the topic:
⇑
Related topics
- Run-time type information (RTTI). Statements is, as. Examples
- Reflection of types. Getting type metadata. The System.Reflection namespace. Class System.Type. Ways to get information about types
- Examples of obtaining information about methods, interfaces, classes, structures, enumerations, delegates, type fields, method parameters, type statistics