为什么我的班子对隐式转换器的表现如此怪异

时间:2018-12-14 13:22:58

标签: c# casting

让我说我上课:

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;不编译?

0 个答案:

没有答案