如果我在await
中使用method aaa()
,它将返回到MainWindow_Loaded
。我要执行以下代码。
[]
这是我的ExceptionAspect
代码
[]
我如何执行以下代码? 预先谢谢你。
public partial class MainWindow : Window
{
private string 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");
}
}
}
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ExceptionAspectAttribute : OnExceptionAspect
{
public 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;
}
}