PostSharp:OnMethodBoundaryAspect 不会被调用

发布于 2024-08-04 04:39:18 字数 1632 浏览 9 评论 0原文

我正在使用PostSharp 将CompoundAspect 应用于ActiveRecord 类(来自CastleProject)。代码如下所示:

public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection)
{
    Type targetType = (Type)targetElement;
    RevertibleSubAspect revertible = new RevertibleSubAspect();
    revertible.Cascade = this.Cascade;
    collection.AddAspect(targetType, revertible);

    //This isn't working
    MethodInfo saveMethod = targetType.GetMethod("Save");
    collection.AddAspect(saveMethod, new CommitOnSaveSubAspect());

    foreach (PropertyInfo property in targetType.GetProperties())
    {
        if((this.Only != null && this.Only.IndexOf(property.Name) == -1) ||
           (this.Except != null && this.Except.IndexOf(property.Name) > -1))
        {
            continue;
        }

        if (property.DeclaringType == targetType && property.CanWrite)
        {
            MethodInfo method = property.GetSetMethod();
            if (method != null && !method.IsStatic)
            {
                collection.AddAspect(method, new TrackInitialPropertyValuesSubAspect());
            }
        }
    }
}

一切正常,除了 CommitOnSaveSubAspect 之外,它是 OnMethodBoundaryAspect。调用 Save 方法时,永远不会调用 OnSuccess 方法。我已经尝试将代码移至 OnEntry 和 OnExit 但这里情况相同。

CommitOnSaveSubAspect 类如下所示:

[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionEventArgs eventArgs)
    {
        ((IRevertible)eventArgs.Instance).Commit();
    }
}

我应用方面的方式是否错误?

I'm using PostSharp to apply a CompoundAspect to a ActiveRecord class (from CastleProject). The code looks like this:

public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection)
{
    Type targetType = (Type)targetElement;
    RevertibleSubAspect revertible = new RevertibleSubAspect();
    revertible.Cascade = this.Cascade;
    collection.AddAspect(targetType, revertible);

    //This isn't working
    MethodInfo saveMethod = targetType.GetMethod("Save");
    collection.AddAspect(saveMethod, new CommitOnSaveSubAspect());

    foreach (PropertyInfo property in targetType.GetProperties())
    {
        if((this.Only != null && this.Only.IndexOf(property.Name) == -1) ||
           (this.Except != null && this.Except.IndexOf(property.Name) > -1))
        {
            continue;
        }

        if (property.DeclaringType == targetType && property.CanWrite)
        {
            MethodInfo method = property.GetSetMethod();
            if (method != null && !method.IsStatic)
            {
                collection.AddAspect(method, new TrackInitialPropertyValuesSubAspect());
            }
        }
    }
}

Everything works fine, except the CommitOnSaveSubAspect which is a OnMethodBoundaryAspect. The OnSuccess method never gets called when the Save method is invoked. I have already tried moving the code to OnEntry and OnExit but same situation here.

The CommitOnSaveSubAspect class looks like this:

[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionEventArgs eventArgs)
    {
        ((IRevertible)eventArgs.Instance).Commit();
    }
}

Am I applying the aspect the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猫九 2024-08-11 04:39:18

调试方面的一个好方法是使用 Reflector 查看生成的程序集。方法是否如您所期望的那样得到增强?

您还可以通过在其中放置断点并使用以下命令行运行 msbuild 来调试 ProvideAspects 方法:

msbuild /p:PostSharpAttachDebugger=true

A good way to debug an aspect is to look at the resulting assembly using Reflector. Are methods enhanced as you expect?

You can also debug the ProvideAspects method by putting a breakpoint into it and running msbuild with the following command line:

msbuild /p:PostSharpAttachDebugger=true
是伱的 2024-08-11 04:39:18

PostSharp 在安装时是否已全局定义?否则,您必须编辑项目文件才能将 PostSharp 正确注入到程序集中。

请参阅http://doc.postsharp。 org/1.0/Default.aspx##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html

Was PostSharp defined globally when you installed it? Otherwise, you'll have to edit your project files in order for PostSharp to be injected properly into your assemblies.

See http://doc.postsharp.org/1.0/Default.aspx##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html.

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