Emit Learning (1), emit Learning
Dapper (Emit used), CYQ. after Data (another way of thinking, without using Emit) framework, I also want to create a small framework for myself to play with it. However, at this time, the ability is too short to do Cyq. data or PDF. net Framework, so the learning path has started. first, set a small goal that can be achieved, starting with Emit.
I. Application scenarios
Emit is applicable in the following scenarios:
1. dynamically create types, modules, and so on in the running process, and at the same time improve the efficiency (you can dynamically compile it once, and then you don't have to deal with it again ).
2. Delayed Use of bound objects.
3. Development of tool plug-ins and IDE.
4. Implementation of ORM.
5. Reduce the performance loss of reflection.
Ii. Small Example
The following part is reproduced from: http://www.cnblogs.com/gjhjoy/p/3627243.html (if Infringing, please contact me to delete this part of content)
Class Program {static void Main (string [] args) {// 1. build the Assembly var asmName = new AssemblyName ("Test"); var asmBuilder = AppDomain. currentDomain. defineDynamicAssembly (asmName, AssemblyBuilderAccess. runAndSave); // 2. create module var mdlBldr = asmBuilder. defineDynamicModule ("Elvinle", "Elvinle.exe"); // 3. defines the class var typeBldr = mdlBldr. defineType ("Hello", TypeAttributes. public); // 4. define class members (methods, attributes, etc.) // public void SayHello () {} var methodBldr = typeBldr. defineMethod ("SayHello", MethodAttributes. public | MethodAttributes. static, null, null); // 5. build method body // obtain the il generator var il = methodBldr. getILGenerator (); il. emit (OpCodes. ldstr, "Hello, HelloWorld"); il. emit (OpCodes. call, typeof (Console ). getMethod ("WriteLine", new Type [] {typeof (string)}); il. emit (OpCodes. call, typeof (Console ). getMethod ("ReadLine"); il. emit (OpCodes. pop); // The read value will be pushed to the evaluation stack, and this method has no returned value. Therefore, you need to discard the value on the stack il. emit (OpCodes. ret); // call CreateType to create the type var t = typeBldr. createType (); // set the entry point to SayHello (), which is equivalent to Main () asmBuilder. setEntryPoint (t. getMethod ("SayHello"); asmBuilder. save ("Elvinle.exe ");}}
View Code
The following part is reproduced from: http://www.cnblogs.com/Mervin/archive/2013/04/05/reflection-3.html (if Infringing, please contact me to delete this part of content)
Several common Emit classes are as follows:
Class Name |
Purpose |
AssemblyBuilder |
Used to define and create dynamic assembly |
ConstructorBuilder |
Constructors used to create dynamic classes |
CustomAttributeBuilder |
Used to create custom features |
MethodBuilder |
Methods used to create dynamic classes or constructor, because constructor itself is also a special method. |
ModuleBuilder |
Used to create modules in a dynamic assembly |
TypeBuilder |
Used to define and create new instances of dynamic classes |
DynamicMethod |
Used to create dynamic methods for dynamic compilation and execution |
ILGenerator |
Used to generate an intermediate language, that is, MSIL commands |
OpCodes |
Field representation of Microsoft intermediate language (MSIL) Instructions |
First, let's take a look at the general steps for using Emit:
1. Create an assembly.
2. Create a module in the Assembly.
3. Create a dynamic class in the module.
4. Add dynamic methods, attributes, events, and so on to dynamic classes.
5. Generate the relevant IL code.
6. Return the created type or save it to the hard disk persistently.
Iii. Command details, similar to assembly
Since the English version is installed on the computer vs, some of the OpCodes commands are not very understandable. So attach a Chinese version.
Address: http://files.cnblogs.com/files/elvinle/OpCodes.rar
Next article: Emit Learning (2)-IL