PostSharp等待执行以下代码

时间:2018-12-17 06:11:44

标签: .net async-await aop postsharp

我使用postsharp处理try catch,上一篇文章中有一个问题,我重新发送它
    公共类ExceptionAspectAttribute:OnExceptionAspect     {         公共ExceptionAspectAttribute()         {             ApplyToStateMachine = true;         }

    public override void OnException(MethodExecutionArgs args)
    {
        var msg =
            $"时间[{DateTime.Now:yyyy年MM月dd日 HH时mm分}]方法{args.Method.Name}发生异常: {args.Exception.Message}\n{args.Exception.StackTrace}";
        Debug.WriteLine(msg);
        args.ReturnValue = Task.FromResult(true);
        args.FlowBehavior = FlowBehavior.Continue;
   }
}

我的代码

public partial class MainWindow : Window

{     私有字符串a;

[ExceptionAspect]
private async Task<object> Init()
{
    a.GetTypeCode();//throw Exception
    return null;
}

public MainWindow()
{
    InitializeComponent();
    this.Loaded += MainWindow_Loaded;
}

private  void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    aaa();
    Debug.WriteLine("do something!");
}

public async  void aaa()
{
     //This code executes and returns to MainWindow_Loaded,i want code keep going
     var a= await Init();

    //i want to Execute the following code
    if (a == null)
    {
        Debug.WriteLine("the following code");
    }
}

}

我想执行Init()方法,然后继续执行

 if (a == null)
        {
            Debug.WriteLine("the following code");
        }

但是它返回到MainWindow_Loaded(),我应该如何使用postsharp 预先谢谢你

0 个答案:

没有答案