代码之家  ›  专栏  ›  技术社区  ›  Hand-E-Food

如何设置ApplyToStateMachine?

  •  0
  • Hand-E-Food  · 技术社区  · 6 年前

    我第一次在桌面应用程序中使用PostSharp 5.0.35进行诊断日志记录。我添加了示例代码并调用了 Initialise Program.Main()

    using PostSharp.Patterns.Diagnostics;
    using PostSharp.Patterns.Diagnostics.Backends.Log4Net;
    using PostSharp.Extensibility;
    
    [assembly: Log(
        AttributePriority = 1,
        AttributeTargetMemberAttributes = MulticastAttributes.Protected | MulticastAttributes.Internal | MulticastAttributes.Public
    )]
    [assembly: Log(
        AttributePriority = 2,
        AttributeExclude = true,
        AttributeTargetMembers = "get_*"
    )]
    
    class AspectInitialiser
    {
        public void Initialise()
        {
            LoggingServices.DefaultBackend = new Log4NetLoggingBackend();
        }
    }
    

    由于代码具有以下错误消息,因此代码不会编译 async 方法。NET Framework 4.0项目。

    当前目标框架不支持将方面应用于异步状态机。将方面应用于异步方法时,请将ApplyToStateMachine属性设置为false。

    很好,但是这个在哪里 ApplyToStateMachine 所有物我能找到的唯一文档假设我知道该房产在哪里。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Hand-E-Food    6 年前

    我收到了PostSharp的官方回复,解决了这个问题。


    你好

    我知道在你的项目中你正在使用 Microsoft.Bcl.Async 在中提供异步支持的包。NET Framework 4.0。这个 微软Bcl。异步 PostSharp不支持包,因此在此配置中无法处理异步方法。不幸的是,我们发出的错误消息非常普遍,必须加以改进 LogAttribute 尤其是不暴露 ApplyToStateMachine 不动产。

    作为一种解决方法,您可以禁用 CompileTimeValidate 方法下面的示例演示了这种方法。

    public class MyLogAttribute : LogAttribute
    {
        public override bool CompileTimeValidate(MethodBase method)
        {
            // C# compiler marks async methods with AsyncStateMachineAttribute
            if (method.GetCustomAttributes(typeof(AsyncStateMachineAttribute), false).Length > 0)
                return false;
    
            return true;
        }
    }