Aspect Injector 文档——注入切面

In the AspectInjector, injection is done by applying the trigger attribute to a target. Trigger is created by defining .net attribute class and marking it with [Injection] attribute.

在 AspectInjector 中,通过将[trigger]特性应用于目标来完成注入。Trigger被定义为.net特性类并用[Injection]特性标记它。

1 // defining aspect
2 [Aspect(Scope.Global)]
3 class LogAspect {}
4 
5 // defining a trigger
6 [Injection(typeof(LogAspect))]
7 class Log : Attribute {}
8 //

After the trigger is defined you can apply it to the target:

定义了trigger之后,你可以在目标上应用此trigger:

1 class TestClass {
2     [Log]
3     public void DoSomething() {}
4 }

However the trigger is only an "attempt" to inject something into the target. When the trigger is fired for an aspect and the target then it is up to the aspect to decide if anything is injected. For instance if the aspect effects are configured to deal only with public members and the target doesn't have such then nothing is injected.

然而trigger只是注入的“尝试”。当trigger为某个aspect和目标触发时,是否注入任何东西取决于aspect。例如,如果aspect effects被配置为只处理公共成员,而目标没有公共成员,那么就不会注入任何东西。

Multi-trigger  多trigger

Triggers can be defined to inject multiple aspects at a time.

可以定义Triggers来一次注入多个aspects。

 1 // defining log aspect
 2 [Aspect(Scope.Global)]
 3 class LogAspect {}
 4 
 5 // defining measure execution time aspect
 6 [Aspect(Scope.Global)]
 7 class MeasureAspect {}
 8 
 9 // defining a trigger
10 [Injection(typeof(LogAspect))]
11 [Injection(typeof(MeasureAspect))]
12 class LogAll : Attribute {}

Injection propagation  注入传播

Trigger propagates injection to submembers of a target. This behaviour will be customizable in future versions.

Trigger 将注入传播到目标的子成员。这种行为在将来的版本中是可定制的。

 1 [Log]
 2 class TestClass {
 3     public void DoSomething() {}
 4     public void DoSomethingElse() {}
 5 }
 6 
 7 // equals to
 8 
 9 class TestClass {
10     [Log]
11     public void DoSomething() {}
12     [Log]
13     public void DoSomethingElse() {}
14 }
15 
16 // and in case your dll has the only class - equals to
17 [assembly: Log]

Triggers and parameters  Triggers与参数

Triggers can carry parameters which can be read by an Aspect to execute additional logic.

Trggers可以携带参数,Aspect可以读取这些参数以执行其他逻辑。

 1 [Injection(typeof(LogAspect))]
 2 class Log : Attribute {
 3 
 4     public Level Level { get; }
 5 
 6     public Log (LogLevel level){
 7         Level = level;
 8     }
 9 }
10 
11 class TestClass {
12     [Log(Level.Warning)]
13     public void DoSomething() {}
14 }

Injection suppression  注入禁止

If there is a need to skip injection in some cases, there is an attribute SkipInjection, which can annotate classes, constructors, methods, properties, and events. Once applied, it suppresses any injections to the target.

如果在某些情况下需要跳过注入,有一个[SkipInjection]特性可供使用,它可以装饰类,构造函数,方法,属性和事件。一旦应用此特性,它会禁止目标上的任何注入。

Please note that inheritance is not taken into account - applying the attribute to a base class doesn't suppress injections to its descendants.

请注意,继承不在考虑范围之内——为基类应用此特性,不会在其子类上禁止注入。

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