Aspect Injector 文档——通知效果参数
Advice method can accept various arguments that represent information about target and triggers and can be used in complex scenarios.
Advice方法可以接受各种参数(这些参数表示关于目标和triggers的信息),并可用于复杂的场景。
Name
Name argument carries a name of a target. If the target is a method, then the name will be the name of the method. If injection is applied to a whole property or an event then the name will be the name of the property or the event.
Name参数携带目标的名称。如果目标是一个方法,那么name将是该方法的名称。如果注入应用于整个属性或事件,则name将是属性或事件的名称。
1 class LogAspect { 2 [Advice(Kind.Before, Targets = Target.Method)] 3 public void LogEnter([Argument(Source.Name)] string name) 4 { 5 Console.WriteLine($"Entering method '{name}'."); 6 } 7 }
Type
Type argument carries a type that contains the target method.
Type参数携带包含目标方法的类型。
1 class CountCreationAspect { 2 private int _count = 0; 3 [Advice(Kind.Before, Targets = Target.Constructor)] 4 public void LogEnter([Argument(Source.Type)] Type type) 5 { 6 Console.WriteLine($"Instance of type {type.Name} created {++_count} times."); 7 } 8 }
Instance
Instance argument has a reference to the instance that owns target method. Note that the reference can be null
when the target method is static.
Instance参数具有对拥有目标方法的实例的引用。注意,当目标方法是静态的时候,引用为 null。
1 class FreezableAspect { 2 [Advice(Kind.Before, Targets = Target.Public | Target.Setter)] 3 public void CheckIfFrozen([Argument(Source.Instance)] object instance) 4 { 5 if (instance is IFreezable freezable && freezable.IsFrozen) 6 throw new InvalidOperationException("Attempt to modify frozen object."); 7 } 8 }
Metadata (former Method) 元数据(目标方法)
Metadata argument refers to reflection metadata of a target method.
Metadata 参数引用目标方法的反射元数据。
