PostSharp 切入点

发布于 2024-11-19 03:10:06 字数 818 浏览 4 评论 0原文

在开始之前,我想澄清一下,我目前对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-11-26 03:10:06

属性是方法,确切地说是两种方法。请记住,PostSharp 在 MSBuild 将 C# 转换为 MSIL 之后开始工作。

您所做的不是在运行时检查,而是在编译时检查。覆盖 CompiletimeValidate() 方法并将检查代码移至此处。如果方法匹配,则返回 false,否则返回 true。 PostSharp 使用此方法来确定(在编译时)是否将方面应用于目标。运行时不必发生任何事情。

您还可以使用多播来完成此操作。请参阅以下 PostSharp 校长文章,了解有关 http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx

[assembly: MyAspect(AspectPriority = 10)] 
[assembly: MyAspect(AspectPriority = 0,  
    AttributeExclude = true, AttributeTargetMembers = "regex:get_.*|set_.*")]

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

[assembly: MyAspect(AspectPriority = 10)] 
[assembly: MyAspect(AspectPriority = 0,  
    AttributeExclude = true, AttributeTargetMembers = "regex:get_.*|set_.*")]

MethodPointcut (along with a few others) is meant for complex aspects. There will be an article coming out about these next week.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文