|
|
Posted on 2021-3-21 12:38:34
|
|
|
|

Application scenarios, by our C# code, dynamically generate an EXE, its application scenarios can be very many, such as software authorization, you can enter the authorization information, generate an authorized DLL, etc., so how to achieve this function, we must mention a technical Emit.
1. Overview of Emit
Emit, can be called emitting or generating. In Framework, classes related to Emit basically exist under the System.Reflection.Emit namespace. It can be seen that Emit exists as an element of reflection. Speaking of reflection, everyone should be familiar with it, it allows us to view the elements of the assembly, so as to obtain a large amount of information such as what types the assembly contains, what methods the type contains, and so on. But reflections can only be 'seen', while Emit can dynamically generate code at runtime. Let's take a look at how to generate code with Emit.
2. Assembly and Managed Module
An assembly is a logical grouping of one or more modules, resource files, and secondarily an assembly is the smallest unit of reuse, security, and versioning. The DLLs and EXEs we see can be called an assembly, an assembly contains multiple modules, but usually, when we VS compile, we will only compile one module, if we want to compile multiple modules in an assembly, we need to use csc.exe implementation.
3. Dynamically generate code operations
First, we need to understand what type each dynamic type is represented by in .NET.
Assembly: System.Reflection.Emit.AssemblyBuilder (defines and represents dynamic assemblies)
Constructor: System.Reflection.Emit.ConstructorBuilder (a constructor that defines and represents dynamic classes)
Custom Attribute: System.Reflection.Emit.CustomAttributeBuilder (helps generate custom attributes using parameters passed by the constructor to generate attributes for classes)
Enum: System.Reflection.Emit.EnumBuilder (explains and indicates the enum type)
Event: System.Reflection.Emit.EventBuilder (event that defines the class)
Field: System.Reflection.Emit.FieldBuilder (Defines and represents fields.) cannot inherit such a class)
Local variables: System.Reflection.Emit.LocalBuilder (represents local variables within a method or constructor)
Method: System.Reflection.Emit.MethodBuilder (a method (or constructor) that defines and represents a dynamic class)
Module: System.Reflection.Emit.ModuleBuilder (defines and represents modules in dynamic assemblies)
Parameter: System.Reflection.Emit.ParameterBuilder (create or associate parameter information such as method parameters, event parameters, etc.)
Property: System.Reflection.Emit.PropertyBuilder (Define the type of property)
Class: System.Reflection.Emit.TypeBuilder (define and create a new instance of the class at runtime)
OpCode is a description of an intermediate language (IL) directive. There are a lot of instructions for this, you can check the official Microsoft website:The hyperlink login is visible.
AssemblyBuilderAccess access restrictions
AssemblyBuilderAccess.Run; Indicates that the assembly can be executed, but not saved. AssemblyBuilderAccess.Save; Indicates that the assembly can be saved, but not executed. AssemblyBuilderAccess.RunAndSave; Indicates that the assembly can be saved and executed. AssemblyBuilderAccess.ReflectionOnly; Indicates that assemblies can only be used in a reflective context and cannot be executed. AssemblyBuilderAccess.RunAndCollect; Indicates that the assembly can be unloaded and memory is reclaimed.
The code is as follows:
First, use emit to generate IL code, then, dynamically generate an assembly, and finally, load the assembly and call its methods, as shown in the figure below:
Use ILSpy to view the generated code as shown in the image below:
(End)
|
Previous:The difference between the Roslyn MSBuild compilerNext:.NET/C# Reflection, Emit, Expression performance testing
|