PostSharp 切入点
在开始之前,我想澄清一下,我目前对 AOP 术语的理解如下......
- 方面是 AOP 相当于 OOP 中的类。
- 建议在 AOP 中相当于 OOP 中的方法。
- 切入点在 AOP 中相当于 OOP 中的“使用”代码。在 OOP 中,我们调用事物。在 AOP 中我们编织东西。 什么编织哪里的决定由切入点定义。
关于实际问题...
我在 PostSharp 中有一个日志记录方面,我想在每个方法(不包括属性)上使用(编织)。最初我在方面使用以下内容:
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance)]
但是,我发现方面仍然被编织到属性中,这意味着我必须在运行时执行二次检查以阻止我的代码在属性上执行:
if (!methodName.StartsWith("set_") && !methodName.StartsWith("get_")) {
这并不理想。我应该能够在切入点中定义此行为,这样我就不必执行任何运行时检查。
我一直在研究 MethodPointcut
属性,它似乎为我提供了一个回调,以帮助编织者在构建时选择建议的候选者。 我可以看一个例子吗?
假设这确实有效,我仍然在思考“为什么我必须将切入点硬编码到我的建议中?”。方面和建议是定义/实现。切入点就是用法。两者应该分开。
Before I start, I'd like to clarify that my current understanding of AOP terminology is as follows...
- Aspects are the AOP equivalent of Classes in OOP.
- Advices are the AOP equivalent of Methods in OOP.
- Pointcuts are the AOP equivalent of 'using' code in OOP. In OOP we invoke things. In AOP we weave things. The decision of what to weave where is defined by Pointcuts.
Onto the actual question...
I have a logging aspect in PostSharp which I want to use (weave) on every method, excluding properties. Originally I was using the following on my aspect:
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = MulticastAttributes.Instance)]
However, I found the aspect was still being woven into properties, meaning I had to perform a secondary check at runtime to preclude my code from executing on properties:
if (!methodName.StartsWith("set_") && !methodName.StartsWith("get_")) {
This is not ideal. I should be able to define this behavior in my pointcut so that I don't have to perform any runtime checking.
I have been looking into the MethodPointcut
attribute which appears to provide me with a callback to help the weaver select candidates for the advice at build time. Please can I see an example?
Assuming this does work, I am still left thinking 'Why must I hardcode Pointcuts to my Advices?'. Aspects and Advices are the definition/implementation. Pointcuts are the usage. The two should be seperate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性是方法,确切地说是两种方法。请记住,PostSharp 在 MSBuild 将 C# 转换为 MSIL 之后开始工作。
您所做的不是在运行时检查,而是在编译时检查。覆盖 CompiletimeValidate() 方法并将检查代码移至此处。如果方法匹配,则返回 false,否则返回 true。 PostSharp 使用此方法来确定(在编译时)是否将方面应用于目标。运行时不必发生任何事情。
您还可以使用多播来完成此操作。请参阅以下 PostSharp 校长文章,了解有关 http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx
MethodPointcut(以及其他一些)适用于复杂的方面。下周将会有一篇关于这些的文章发表。
Properties ARE methods, two methods to be exact. Remember, PostSharp does it's work AFTER MSBuild turns your C# into MSIL.
What you do is not check at runtime but check at compile time. Override the CompiletimeValidate() method and move your check code there. If the method matches, return false other wise return true. PostSharp uses this method to determine (at compile time) if the aspect will be applied to the target. Nothing has to happen at runtime.
You can also do this using multicasting. See the following PostSharp Principals article for detials on that http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx
MethodPointcut (along with a few others) is meant for complex aspects. There will be an article coming out about these next week.