Aspect Injector 文档——通知效果

Advice effect is a code execution that can be injected into a method and, by extension, into properties and events as they use methods under the hood.

Advice effect是一种代码执行,它可以注入到方法中,也可以通过扩展注入到属性和事件中,因为属性和事件在底层使用方法。

Advice is defined as a method within the aspect class and marked with an [Advice] attribute. This attribute has the only required parameter that determines the kind of the advice. Advice method can also accept various arguments. See Advice Effect Arguments to find out more.

Advice 被定义为aspect类中的一个方法,并用[Advice]特性标记。此特性只有一个必需的参数,它决定advice的类型。Advice方法也可以接受各种参数。更多请看Advice Effect Arguments

 1 class LogAspect {
 2     [Advice(Kind.Before)]
 3     public void LogEnter() {
 4         Console.WriteLine("Entering ...");
 5     }
 6 
 7     [Advice(Kind.After)]
 8     public void LogExit() {
 9         Console.WriteLine("Leaving ...");
10     }
11 
12     [Advice(Kind.Around)]
13     public object LogAndMeasureTimings(...) {
14         ...
15     }
16 }

There are three kind of advice at the moment:

目前有三种advice:

  • Before - the code is injected before the method begins. Special case for constructors - the code is injected before the constructor begins but after the call to base class .ctor is done.  代码被注入到方法开始之前(前端)。构造函数的特殊情况——代码在构造函数开始之前但在基类的构造函数之后注入。
  • After - the code is injected after the method ends.  代码被注入到方法末端。
  • Around - the code is executed instead of the target method. The call to original method is optional and up to advice definition. Refer to Advice Effect Arguments to find out how to make original method call. This advice however has to return an object that will cast to the target method's return type. Even if the target method's return type is void, the advice has to return at least null. Special case for constructors - Around advice cannot be applied around constructors.  代码代替目标方法执行。对原始方法的调用是可选的,并且符合advice定义。参考 Advice Effect Arguments 可找到如何调用原始方法。但是,此advice必须返回可以强制转换为目标方法的返回类型的对象。即使目标方法的返回类型为 void,advice也必须至少返回 null。构造函数的特殊情况——不能在构造函数周围应用 Around advice。

Advice targets  通知目标

Advice can target certain type of members via the second parameter of an [Advice] attribute.

Advice可以通过[Advice]特性的第二个参数指向某种成员。

1 class LogAspect {
2     [Advice(Kind.Before, Targets = Target.Public | Target.Setter)]
3     public void LogEnter() {
4         Console.WriteLine("Entering public property setter...");
5     }
6 }

The Target enumeration has the following options:

Target枚举有以下枚举项:

Complex values:

复合值:

  • Any - All members (AnyMember + AnyAccess + AnyScope) (Default value)  所有成员(任意成员+任意访问方式+任意范围)(缺省值)
  • AnyMember = Members of any type (Default member type value)  任意类型的成员(缺省的成员类型值)
  • AnyAccess - Members of any access (Default member access value)  任意访问方式成员(缺省的成员访问方式值)
  • AnyScope = Members of any scope (Default member scope value)  任意范围成员(缺少的成员范围值)

Access values:

访问方式:

  • Private - Private members  私有成员
  • Internal - Internal members  内部成员
  • Protected - Protected members  受保护成员
  • ProtectedInternal - Protected internal members (Protected OR Internal)  受保护内部成员(受保护或内部)
  • ProtectedPrivate - Protected AND internal members  受保护并内部成员
  • Public - Public members  公有成员

Scope values:

范围值:

  • Static - Static members  静态成员
  • Instance - Non static members  非静态成员

Type values:

类型值:

  • Constructor - Constructors  构造函数
  • Method - Methods  方法
  • Getter - Property getters  属性访问器
  • Setter - Property setters  属性设置器
  • EventAdd - Event subscribe handlers  事件订阅器
  • EventRemove - Event unsubscribe handlers  事件取消订阅器
posted @ 2022-11-30 16:45  菜鸟吊思  阅读(55)  评论(0)    收藏  举报