让我说我上课:
public class Result<TValue,TFailureReason>: ActionResult<TFailureReason>
{
//...
public static implicit operator Result<TValue, TFailureReason>(TValue value)
=> Succeeded(value);
public static implicit operator Result<TValue, TFailureReason>(TFailureReason failureReason)
=> Failed(failureReason);
}
我正在尝试在这里使用它:
public async Task<Result<IAppServicePlan, AzureSdkFailure>> GetAppServicePlanAsync(string name, string resourceGroupName)
{
try
{
IAppServicePlan appServicePlan = await azure.AppServices.AppServicePlans.GetByResourceGroupAsync(resourceGroupName, name, cancellationToken)
.ConfigureAwait(false);
return appServicePlan;
} catch(Exception ex)
{
return new AzureSdkFailure(ex);
}
}
但是我得到了编译错误:
从“ IAppServicePlan”到“结果”。一个 存在显式转换(您是否缺少演员表?)
但是,当我更改为此
时:public async Task<Result<IAppServicePlan, AzureSdkFailure>> GetAppServicePlanAsync(string name, string resourceGroupName)
{
try
{
IAppServicePlan appServicePlan = await azure.AppServices.AppServicePlans.GetByResourceGroupAsync(resourceGroupName, name, cancellationToken)
.ConfigureAwait(false);
return Result<IAppServicePlan, AzureSdkFailure>.Succeeded(appServicePlan);
} catch(Exception ex)
{
return new AzureSdkFailure(ex);
}
}
它工作正常。
为什么return new AzureSdkFailure(ex);
编译而return appServicePlan;
不编译?