PostSharp 根本不会捕获异常

Exceptions are simply not caught by PostSharp

嗯,我不明白。尝试将 PostSharp 属性应用于方法的异常处理,无论如何都无法达到方面的 OnException 方法。这是非常令人失望的。 为了测试,假设这是方面:

[PSerializable]
public class HandleExceptionsAttribute : OnExceptionAspect
{
    public override void OnException(MethodExecutionArgs args)
    {
        Trace.TraceError(args.Exception.Message);            
        args.FlowBehavior = FlowBehavior.Return;
    }
}

并且应用于只抛出异常的方法:

[HandleExceptions]
public static void Test()
{
    throw new Exception("Test");
}

好吧,什么也没发生。方面的 OnException 方法根本没有达到(通过调试或其他方式)。

我错过了什么?

安装 PostSharp NuGet 包后,应该可以访问方面代码,但可能无法访问方面代码中的 break-points。您需要安装 Visual Studio 的 PostSharp 工具,才能调试方面代码。您可以在 https://www.postsharp.net/download.

下载