PostSharp:方法***应该使用选择器自定义属性进行注释,因为它是主处理程序

问题描述 投票:1回答:1

此代码抛出错误:Method ...OnEntry(...) should be annotated with a selector custom attribute because it is a master handler

[PSerializable]
public class LogRequestAttribute : MethodLevelAspect, IAspectProvider {
    public IEnumerable<AspectInstance> ProvideAspects(object target) {
        yield return new AspectInstance( target, new LogPlainRequest() );
    }
}

[PSerializable]
public class LogPlainRequest : IMethodLevelAspect {
    public void RuntimeInitialize(MethodBase method) {
    }
    [OnMethodEntryAdvice]
    public void OnEntry(MethodExecutionArgs args) {
    }
}

什么是错误含义?有什么问题?

postsharp
1个回答
1
投票

您可以将几个相关的建议组合成一个组(例如OnEntryOnExit)。这就是OnMethodBoundaryAspect自动为您做的事情。在对建议进行分组后,您需要将其中一个指定为“主要建议”。必须在主建议上设置组的属性和切入点。

分配给主建议的切入点充当建议的目标元素的选择器。例如,SelfPointcut只是选择方面的目标作为建议的目标。您可以在文档中找到更多信息和不同的切入点类型:https://doc.postsharp.net/advices

在上面的示例中,您可以将[SelfPointcut]属性应用于OnEntry方法以消除错误消息。

[PSerializable]
public class LogPlainRequest : IMethodLevelAspect {
    public void RuntimeInitialize(MethodBase method) {
    }

    [OnMethodEntryAdvice]
    [SelfPointcut]
    public void OnEntry(MethodExecutionArgs args) {
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.