多应用于客户端。
if (_instance == null)
{
_instance = await MyAsyncClass.CreateAsync();
}
_instance.Test();
class MyAsyncClass
{
private MyAsyncClass() { }
private async Task<MyAsyncClass> InitializeAsync()
{
await Task.Delay(TimeSpan.FromSeconds(2));
return this;
}
public static Task<MyAsyncClass> CreateAsync()
{
var result = new MyAsyncClass();
return result.InitializeAsync();
}
public void Test()
{
Console.WriteLine("需要构造初始化完成才能操作");
}
}