PostSharp OnMethodBoundaryAspect OnEntry Not Executing

时间:2011-06-16 20:18:57

标签: c# .net-4.0 aop postsharp

我正在运行.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实例化。

1 个答案:

答案 0 :(得分:2)

是的,每个开发人员都需要安装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);
        }
    }