Aspect Injector 文档——它如何工作

AspectInjector is a compile-time AOP framework which means that all required work is done at compile time. For example, before compilation your code looks like:

AspectInjector 是一个编译时 AOP 框架,这意味着所有必需的工作都是在编译时完成的。例如,在编译之前,您的代码看起来像:

 1 [Aspect(Scope.Global)]
 2 [Injection(typeof(Log))]
 3 class Log : Attribute
 4 {
 5     [Advice(Kind.Before, Targets = Target.Method)]
 6     public void OnEntry([Argument(Source.Name)] string name)
 7     {
 8         Console.WriteLine($"Entering method {name}");
 9     }
10 }
11 
12 class TestClass
13 {
14     [Log]
15     public void Do()
16     {
17         Console.WriteLine($"Done");
18     }
19 }

Then when you hit F5, we pick up from there and change your assembly a bit, so it actually works like this:

然后当你按F5时,我们从那里开始改变你的程序集,所以它实际上是这样工作的:

 1 [Aspect(Scope.Global)]
 2 [Injection(typeof(Log))]
 3 class Log : Attribute
 4 {
 5     public static readonly Log __a$_instance;
 6 
 7     [Advice(Kind.Before, Targets = Target.Method)]
 8     public void OnEntry([Argument(Source.Name)] string name)
 9     {
10         Console.WriteLine($"Entering method {name}");
11     }
12 
13     static Log()
14     {
15         __a$_instance = new Log();
16     }
17 }
18 
19 internal class TestClass
20 {
21     [Log]
22     public void Do()
23     {
24         Log.__a$_instance.OnEntry("Do");
25         Console.WriteLine("Done");
26     }
27 }

Thus, there is no performance hit as often experienced with other runtime AOP frameworks.

因此,不会像其他运行时 AOP 框架那样经常出现性能问题。

posted @ 2022-11-30 10:50  菜鸟吊思  阅读(203)  评论(0)    收藏  举报