Use reflection. emit (1)

Source: Internet
Author: User
Since the development of AOP. net, we have been using the classes and functions in reflection. emit.
Since emit does not seem to have a lot of Chinese materials, record some usage methods now. Let's take a look.: P

Reflection. emit is usedProgramDynamically generate class and field and method during runtime.
One advantage of this is that dynamicproxy can be generated when the program is running, which can intercept AOP (that is, the implementation principle of AOP. net ).

Emit provides typebuilder (class for generating class), methodbuilder (class for generating method), and fieldbuilder (class for generating class member variables.
Because methodbuilder can only generate function declaration, it cannot generate function executionCodeTo generate a complete function, you must use the ilgenerator class.
Ilgenerator can only add direct il code through the emit (...) function, so you must understand some knowledge about il when using emit.

In programming, function calls are very frequent. Like Win32 assembly, parameters of a function must be pushed to the stack before being called...
In Il, the parameter is required to be pressed to the stack. The parameter should be pushed from left to right, that is, the leftmost parameter of the function declaration should be pushed to the stack first, then press the remaining parameters to the stack. for example:
Function Definition:
Public void show (INT, String, object );

Invokeshow is used to call this function. The definition of invokeshow is as follows:
Public void invokeshow (INT, String, object)

ilgenerator methodil = typebuilder. definemethod (....). getgenerator ();
in addition to the static function, the actual parameters of all other functions will be at the leftmost, that is, this. so the actual parameters here should start from 1.
// assume that the show function's methodinfo is a member variable of the invokeshow class, and its fieldbuilder name is show_methodinfo.
// here, we first press the object to the stack. Because it is a member of the class, we need to add this
methodil. emit (Opcodes. ldarg_0);
methodil. emit (Opcodes. ldfld, show_methodinfo)
// parameter stack entry
methodil. emit (Opcodes. ldarg_1);
methodil. emit (Opcodes. ldarg_2);
methodil. emit (Opcodes. ldarg_3);
// call the function
methodil. emit (Opcodes. callvirt, typeof (methodinfo ). getmethod ("invoke");
// due to methodinfo. the invoke function has a return value, while the invokeshow function does not return a value. Therefore, you must give the return value to pop to keep the stack balance
methodil. emit (Opcodes. pop);
methodil. emit (Opcodes. RET);

The above is an emit Implementation of the simplest function call.
The show_methodinfo of fieldbuilder is used in the above Code. Now let's take a look at how to assign values to this member variable:
The function is defined as follows:
Public void initial (methodinfo showmethod );
Il code:
Methodil. emit (Opcodes. ldarg_0 );
Methodil. emit (Opcodes. ldarg_1 );
Methodil. emit (Opcodes. stdes, show_methodinfo );
Methodil. emit (Opcodes. Ret );

In this way, you can assign values to the member variables of the class ..

So how can we generate temporary variables in the function?
You can use the declarelocal function of ilgenerator to generate temporary variables:
Localbuilder local = methodil. declarelocal (typeof (object ));
// Assign a value to the parameter and pass the value of the first parameter to the local
Methodil. emit (Opcodes. ldarg_1 );
Methodil. emit (Opcodes. stloc, local );
// Use this variable
Methodil. emit (Opcodes. ldloc, local );

By default, the defined localbuilder starts from 0 in order. That is to say, if local is the first localbuilder defined, the code above can also be written as follows:
Methodil. emit (Opcodes. ldarg_1 );
Methodil. emit (Opcodes. stloc, _ 0 );
// Use this variable
Methodil. emit (Opcodes. ldloc_0 );

This is the use of some basic emit...: P

To be continued...

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.