Dry [EMIT] Thousands of lines of code to implement the automatic assembly of proxy-type aop+ properties

Source: Internet
Author: User
Tags emit

Actually speaking of AOP, I sent a post in 2011, CSDN address for http://bbs.csdn.net/topics/370067862, but at that time to emit understand the depth is not enough, so the content seems a little immature, but for me, It means a lot. From that moment on, I counted on the emit to get started.

Don't pull away, get back to the chase. AOP is implemented in 2 different ways, one that is inherited, and this way of implementation requires the injected method to play virtual, such that the class can inherit and rewrite it, as shown in:

The second is proxy-type, the injection of proxy is generally required to have an abstract interface, its approximate implementation of the principle as shown:

The way this article is used is the second, there may be some children's shoes have doubts, why? Here, there is no difference in the emit code inside the method, just the first way only need to rewrite the method with virtual, on emit, relatively simple, and in 2011 that post is based on the first way, so this time want to change the way to go. The second is the need for an interface implementation of the process, a little more difficult, from the point of view of learning, nature is also the best choice.

Speaking of which is only half of the matter, what is the other half? Just now the principle of the agent-type AOP roughly said, will eventually create a dynamic implementation of an interface class, since the laborious creation of a new class, it must be to use it to do things, what is the first step to do things? To pull, is the new instance. This time lazy people (for example I) said, this one of the new more laborious, and the code appears very low, want to tall? This time you think of the IOC or DI (these two nouns do not understand the children's shoes can be self-Baidu), yes, can. But the thought of a small project to lead a bunch of DLLs, but also do not use the inside of several methods, so I create the idea of the wheel came out, the current this can not be compared with the various IOC framework or DI frame, not a magnitude, also compared to others perfect, but for me, just need it to automatically assemble, It's good enough. So in the implementation of the AOP, the automatic assembly of the code is also activated ...

Principle we probably understand, the rest is to do, and hands is actually burning the brain process, all kinds of headache of the IL instructions, in order not to let you children's shoes also headache or see the laborious, the process I do not do a detailed explanation, paste a few can show some of the logic of the important code snippet:

Finally I want to use some test code to show the final effect of this thing, if you want to study emit's children's shoes, according to the comments in my Code, as well as various IL instructions to understand and try to create a wheel.

The three ways to intercept AOP are tested in the test code:

1, general interception, in the implementation of the method before and after the execution of a piece of code

2, authentication interception, only through the authentication interface, in order to call the normal method of proxy class

3, abnormal interception, in the event of an exception, the acquisition and processing of abnormal

The marking of the test class is as follows:

Examples of tests written:

Results of the operation:

Write here also ended, because literary limited, just put I want to say the focus, expressed a bit, most of the content are stickers, there may be some of the children's shoes are not used to see, also please light spray, thank you.

About copyright, because my knowledge from the Internet, can say, no internet, there is no now I, even without my current job, in the heart of Thanksgiving, sincere feedback. So my code here is completely public, do not involve copyright, you can use it (if you have the source code has been modified or extended, and you would like to share with you, I would be grateful), but the consequences of the resulting will be at your own risk. If you want to reprint this article, please also keep the original address, thank you.

Source Address: http://download.csdn.net/detail/nqicecoffee/9592036

Dry [EMIT] Thousands of lines of code to implement the automatic assembly of proxy-type aop+ properties

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.