Postsharp analysis of the anti-compiler

Source: Internet
Author: User

Some time ago to do a permission system, in the consideration of log, abnormal takeover, transaction, caching, and so on some of the landscape components of the architecture analysis, naturally want to use AOP technology to achieve; These two days again study the structure and realization thought of postsharp, integrate it into the existing frame;

Because of the use of MSIL injection, static code injection is more efficient than using Reflection Emit. Using the MSBuild Task allows developers to use AOP as they would with compiler built-in attributes. Arbitrary methods can be intercepted, while the Dynamic Proxy AOP often takes the form of inheritance to intercept the Virtual method. Have more control over it. This includes interrupting the execution process, modifying parameters and return values, and so on. You can also block operations such as Field Access, Exception, and so on. You do not need to change the object creation code to "new proxy ()" to be more transparent. You can use wildcard characters for multiple intercept matches. The problem with static injection is more about the quality of the injected code and the complexity of the debugging. In addition there is a foreigner's using AOP and postsharp to enhance Your Codeab two parts, quite wonderful, this article is based on the reference to these two good articles.

We assume that there is such a scene, in fact, this is the actual business is a very common way to deal with: A certain single management module, with new, delete two functions, we have to check the new deletion of permissions, when the deletion must also log, there are exceptions must also capture and record anomalies;

We can be very troublesome by the previous wording, we have to write like this:

Xorders public class
02 {
public bool Add (string ID, string ordername)
04 {
Try
06 {
Modified if (user.addenable)
08 {
//todo: implementation of new orders
Console.WriteLine ("The operation of the new Order method is being performed, carriage return continues ...");
One console.readline ();
Console.WriteLine ("You add Order Success: No.: {0}, Name: {1}", ID, ordername);
return true;
14}
Else
16 {
17//
18}
19}
catch (Exception)
21 {
//todo: The implementation of record exception
throw;
24}
25
-Return true;
27
28}
29
public bool Delete (string ID)
31 {
Try
33 {
if (user.deleteenable)
35 {
//todo: Delete the implementation of the order
Console.WriteLine ("You delete order Success: No.: {0}", id);
38}
Or else
40 {
41//
42}
43
44}
catch (Exception)
46 {
//todo: The realization of record exception
throw;
49}
50
I return true;
52}

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.