The first time AOP was attached with the DEMO, which is currently only for learning and cannot be used in the production environment. aopdemo

Source: Internet
Author: User

The first time AOP was attached with the DEMO, which is currently only for learning and cannot be used in the production environment. aopdemo

GIT Code address: https://git.oschina.net/ruanjianfeng/Ruan.Framework.Core

The demo code is as follows:

 1     public class ConsoleTimeAttribute : ApectBaseAttribute 2     { 3         public override void Before(ApectContext apectContext) 4         { 5             apectContext.SetCorrelationObject(DateTime.Now); 6             Console.WriteLine($"Before > Method:{apectContext.MethodName} > {DateTime.Now}"); 7         } 8  9         public override void After(ApectContext apectContext)10         {11             Console.WriteLine($"Before > Method:{apectContext.MethodName}  > {DateTime.Now}");12             Console.WriteLine($"CorrelationObject > {(DateTime)apectContext.CorrelationObject}");13         }14     }

First, the feature class that inherits ApectBaseAttribute will be used;

1 public interface ITest2 {3 [ConsoleTime] // added feature monitoring 4 void Say (string message); 5}
    public class Test : ITest    {        private readonly string _init;        public Test(string init)        {            _init = init;        }        public void Say(string message)        {            Console.WriteLine($"init : {_init} || message : {message}");        }    }

How to Use AOP

Var type = ApectProxyBuilder. buildType <ITest> (typeof (Test); ITest test = (ITest) Activator. createInstance (type, "OK"); test. say ("what to Say ");

If IOC is used in combination, you can only provide learning at present. There is still a lack of returned values for the execution methods in the AOP proxy class, and only the AOP processing of non-inherited interface classes.

If you have any questions or problems, please contact us more and use them in the production environment later.

 

Related Article

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.