Basic operation of emit

Source: Internet
Author: User
Tags aop emit

Recently received a message from the readers of the. NET security revelation, mention of the book many people want to see the content is weakened, I would like to reply to a lot of content because the book's main theme or chapter planning is only the outline of the reasons, but on second thought, the reader needs, is not the author should write it? So I'm going to move all the questions in the mail to my blog and share them with you in the form of posts.

The topic to be discussed today is emit, the reflection of the twin brother. It is also very difficult to explain emit through a few blogs, and this series plans to write three blog posts by completing the function of a simple mock interface:

2) say Emit (middle) ilgenerator;

<p cxsplast "style="; margin-left:45pt ">3" emit (lower) emit application in AOP and unit testing;

These blogs can't cover all emit content, just want to let you know what emit is, what basic functions, how to use.

1.1 Technology requirements for dynamic interface implementation

The first requirement to dynamically implement interfaces is what I've encountered in development, and the specific business scenarios are detailed in the "talking about emit (bottom) emit application in AOP and unit tests," outlining what the code level is to achieve. First we have a number of methods similar to the one shown in Figure 1, with before and after endings.

Fig. 1 Several methods for each other

We classify the methods shown above according to certain rules (the rules of classification are not mentioned), in the actual call process, will not call the above method directly, but call an instance of the interface named Iassessmentaopadviceprovider, which is defined as follows:

Publicinterfaceiassessmentaopadviceprovider

{

Object before (object value);

Object After (object Beforeresult, object value);

}

The factory class that is responsible for creating the interface is defined as follows:

Staticclassadviceproviderfactory

{

Internalstaticiassessmentaopadviceprovider Getprovider (AdviceType AdviceType, string instancename,string funcName, Mvcadvicetype Mvcadvicetype)

{

Creating an instance of an interface

}

}

The responsibility of the plant is to select an instance of a Iassessmentaopadviceprovider interface that is similar to the one in Figure 1, based on the parameters passed in, and then return it to the caller for use. Of course, if you do not use emit can also achieve this requirement, here we only discuss how to use emit to implement.

The first requirement is simply introduced here, and we look at the second requirement. Now I'm going to test a class that relies on Iassessmentaopadviceprovider in a unit test, what do we do to control the behavior of Iassessmentaopadviceprovider? If you've done unit tests, you're sure to think of a mock, and we can use MOQ:

mock<iassessmentaopadviceprovider> assessmentaopadviceprovidermocked = newmock< Iassessmentaopadviceprovider> ();

Assessmentaopadviceprovidermocked.setup (t => T. Before (It.isany<object> ())). Returns (Expectobject);

Now I also want to implement such a function, how to do? Let's not be surprised to realize that the full mock functionality is a framework for a dynamic broker, and I haven't had the ambition to do this, and here to demonstrate emit, I've implemented a mock of the Before method of the Iassessmentaopadviceprovider interface in the simplest way, And only for a special case, only to ensure that this special case can be called. Interested readers can read MOQ's source code.

OK, the technical requirements are over, let's start now!

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.