Aspect Injector 文档——定义切面

One starts to define an Aspect by applying the [Aspect] attribute onto a given class. The class decorated with an aspect cannot be abstractstatic, and generic. Otherwise you'll get an error.

通过将[Aspect]特性应用到给定的类上来开始定义Aspect。用[Aspect]特性修饰的类不能是抽象的、静态的和泛型的。否则你会得到一个错误。

1 [Aspect(Scope.Global)]
2 class Log
3 {
4 }

There are currently two scopes in which aspect can operate:  目前aspect有两种范围(生命周期):

  • Scope.Global - means aspect will operate as singleton.  意味着aspect将以单例的方式提供。
  • Scope.PerInstance - means that every target's class will have its own instance of the aspect. Even if aspect is injected several times into different members of the class, still there will be the only instance of the aspect for this class.  意味着每个目标类将拥有自己的aspect实例。即使aspect被多次注入到类的不同成员中,这个类仍然只有一个aspect的实例。

In addition, your aspect can be created either with a parameterless constructor or factory. Make sure the factory class has a method with the proper signature:

此外,您的aspect可以使用无参数构造函数或工厂创建。确保工厂类拥有一个具有如下签名的方法:

 1 [Aspect(Scope.Global, Factory = typeof(AspectFactory))]
 2 class Log
 3 {
 4 }
 5 
 6 class AspectFactory
 7 {
 8     public static object GetInstance(Type type)
 9     {
10     }
11 }

Next step is to define how your Aspect interacts with injection targets. You can achieve this by using Mixin and/or Advice effects.

下一步是定义Aspect如何与注入目标交互。您可以通过使用 Mixin 和/或 Advice effects来实现这一点。

posted @ 2022-11-30 16:34  菜鸟吊思  阅读(70)  评论(0)    收藏  举报