Article From:https://www.cnblogs.com/stry/p/9967562.html

   In the previous article, we learned about simple OnException Aspect AOP directional entry. In the first section, we will continue our OnMethod Boundary Aspect method behavior entry in the PostSharp AOP series, which isIt is also our usual AOP entry.

   OnMethodBoundaryAspectAs the name implies, it is a cut-in to the method boundary, which is defined as follows:

SNAGHTML20c8c71

Four method boundary points are provided here for us to cut in. We can easily access aspects such as method permissions, execution time, parameter legitimacy, etc.

aspectThe incoming parameter MethodExecution Args gives me the following information, including the Instance attribute of the parent AdviceArgs. The instance method has value, while the static method is null.

image

Here we also need to talk about the attribute FlowBehavior: representing the method execution behavior, which is an enumerated variable:

image

2: Demo of execution time

Let’s practice a method to perform time statistics demo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;

namespace PostSharpDemo
{
    [Serializable]
    public class OnMethodBoundaryAspectDemoAttribute : OnMethodBoundaryAspect
    {
        public bool Enabled
        {
            get;
            set;
        }

        public override void OnEntry(MethodExecutionArgs args)
        {
            if (this.Enabled)
            {
                args.MethodExecutionTag = System.Diagnostics.Stopwatch.StartNew();
            }
        }
        public override void OnExit(MethodExecutionArgs args)
        {
            if (this.Enabled)
            {
                var sw = args.MethodExecutionTag as System.Diagnostics.Stopwatch;
                if (sw != null)
                {
                    sw.Stop();
                    Console.WriteLine(String.Format(Method {0} execution time is: {1} s, args.Method.Name, sw.ElapsedMilliseconds / 1000));
                    sw = null;
                }
            }
        }
    }
}

Test methods:

[OnMethodBoundaryAspectDemoAttribute(Enabled=true)] 
       public static void OnMethodBoundaryAspectDemoAttributeTest() 
       { 
           System.Threading.Thread.Sleep(2000); 
       }

The results are as follows:

image

Note: Here we can also use the Multicasting we mentioned in the previous section to add all our methods to our class, assembly.

In the last nonsense, we can easily specify access rights for our methods (such as making our WCF service operation contract), such as the processing of function based on operation rights, such as [PowerAttribute (“Add, Edit”)]In this simple way, we just need to decide the FlowBehavior behavior of aspect in OnEnter, and teach you the rest.

   Welcome to actively correct and communicate.

Attachment: demo download

Other AOP reference:

  • AOPPostSharp Preliminary View – OnException Aspect
  • AOPPostSharp2-On Method Boundary Aspect
  • AOPPostSharp3-Method Interception Aspect
  • AOPPostSharp4-Implementing INotifyPropertyChanged-like Implantation
  • AOPPostSharp5-Location Interception Aspect
  • http://www.cnblogs.com/whitewolf/category/312638.html
     

     

==================================The original link === This article is forwarded by the blog forwarding widget =================================================

Leave a Reply

Your email address will not be published. Required fields are marked *