C# 正在异步任务中返回枚举<;Enum>;并获得价值
我希望根据异步方法的结果返回枚举C# 正在异步任务中返回枚举<;Enum>;并获得价值,c#,reflection,enums,async-await,task,C#,Reflection,Enums,Async Await,Task,我希望根据异步方法的结果返回枚举 public enum ReponseType { Success, Error } 以下是返回响应类型的方法: public async Task<ReponseType> MethodThatDoesStuff() { await Task.Run(() => { //Doing stuff here return Respo
public enum ReponseType
{
Success,
Error
}
以下是返回响应类型的方法:
public async Task<ReponseType> MethodThatDoesStuff()
{
await Task.Run(() =>
{
//Doing stuff here
return ResponseType.Success;
});
return ReponseType.Error;
}
即使我使用:
ResponseType resp = await _writer.MethodThatDoesStuff();
我仍然无法获取枚举值
resp.[intellisense]只给我GetType()、GetTypeCode()、CompareTo()等
如果我只想知道它是成功的还是错误的,那么像这样返回一个枚举不是很好/有效吗
最好的方法是什么
谢谢返回枚举非常好。但是,在这种情况下,您的值将始终是错误的,因为您没有从运行中返回值。您应该执行以下操作:
public Task<ReponseType> MethodThatDoesStuff()
{
return Task.Run(() =>
{
//Doing stuff here
return ReponseType.Success;
});
}
@ScottChamberlain问题不是说结果总是一个错误,问题是结果根本不能被使用。这个问题是不完整的。提供一个可用于重现问题的方法。您希望在IntelliSense中看到什么?如果要返回枚举,请将其与另一个枚举进行比较:if(ResponseType.Success==resp)//Success else//errorOk,谢谢。是的,刚刚测试了响应,它包含了值。无法通过智能感知思想获得,而这正是我的想法。谢谢你的帮助。
public Task<ReponseType> MethodThatDoesStuff()
{
return Task.Run(() =>
{
//Doing stuff here
return ReponseType.Success;
});
}
ResponseType response = await MethodThatDoesStuff();