PostSharp OnMethodBoundaryAspect OnEntry 未执行

发布于 2024-11-16 02:21:22 字数 821 浏览 5 评论 0原文

我正在运行 .NET 4.0 Web 应用程序(不是网站)和 PostSharp 1.5。我无法使用 OnMethodBoundaryAspect 基类执行 OnEntry 重写方法。这是一些相关代码:

public sealed class MonitorAttribute : OnMethodBoundaryAspect {

    public string[] SomeValue { get; protected set; }         

    public MonitorAttribute (params string[] someValue){
        SomeValue = someValue;
    }

    public override void OnEntry(MethodExecutionEventArgs eventArgs){
        // do Something here
        base.OnEntry(eventArgs);
    }

}

public sealed class MyUsageClass : IMyUsageClass {

    [Monitor(new string[]{ 'Test' })
    public void SomeMethod {
        // Do something else in here
    }        

}

我错过了什么吗?它永远不会命中 OnEntry 方法。我还尝试用新的 2.0 版本替换我的 PostSharp.dll 和 PostSharp.Laos.dll 依赖项。如果有什么区别的话,MyUsageClass 由 StructureMap 实例化。

I am running a .NET 4.0 Web Application (not web site) and PostSharp 1.5. I cannot get the OnEntry override method to execute using the OnMethodBoundaryAspect base class. Here is some relevant code:

public sealed class MonitorAttribute : OnMethodBoundaryAspect {

    public string[] SomeValue { get; protected set; }         

    public MonitorAttribute (params string[] someValue){
        SomeValue = someValue;
    }

    public override void OnEntry(MethodExecutionEventArgs eventArgs){
        // do Something here
        base.OnEntry(eventArgs);
    }

}

public sealed class MyUsageClass : IMyUsageClass {

    [Monitor(new string[]{ 'Test' })
    public void SomeMethod {
        // Do something else in here
    }        

}

Am I missing something? It never hits the OnEntry method. I also tried replacing my PostSharp.dll and PostSharp.Laos.dll dependencies with the new 2.0 version. If it makes any difference MyUsageClass is instantiated by StructureMap.

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

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

发布评论

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

评论(1

止于盛夏 2024-11-23 02:21:22

是的,每个开发人员都需要安装 PostSharp。如果您只是使用入门版,那么一切都是免费的。

将此作为答案发布以向您展示代码。我的测试代码

class Program
    {
        [Monitor]
        static void Main(string[] args)
        {

        }
    }

    [Serializable]
    public class MonitorAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("OnEntry");
        }
    }

编译后的代码

internal class Program
    {
        [CompilerGenerated, DebuggerNonUserCode]
        internal sealed class <>z__Aspects
        {
            internal static MethodBase m1 = MethodBase.GetMethodFromHandle(ldtoken(Main()));
            internal static readonly MonitorAttribute a0 = (MonitorAttribute)<>z__AspectsImplementationDetails.aspects1[0];
        }
        private static void Main(string[] args)
        {
            Program.<>z__Aspects.a0.OnEntry(null);
        }
    }

Yes, every dev will need to have PostSharp installed. If you're just using the starter edition then it's all free.

Posting this as an answer to show you the code. My test code

class Program
    {
        [Monitor]
        static void Main(string[] args)
        {

        }
    }

    [Serializable]
    public class MonitorAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("OnEntry");
        }
    }

The code after compilation

internal class Program
    {
        [CompilerGenerated, DebuggerNonUserCode]
        internal sealed class <>z__Aspects
        {
            internal static MethodBase m1 = MethodBase.GetMethodFromHandle(ldtoken(Main()));
            internal static readonly MonitorAttribute a0 = (MonitorAttribute)<>z__AspectsImplementationDetails.aspects1[0];
        }
        private static void Main(string[] args)
        {
            Program.<>z__Aspects.a0.OnEntry(null);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文