相关疑难解决方法(0)

没有赋值的隐式转换?

保留的问题 - 请参阅底部的编辑
我正在开发一个小型函数库,基本上是通过隐藏基本的圈复杂度来提供一些可读性.调用提供程序(调用Select<T>帮助程序工厂Select),用法类似于

public Guid? GetPropertyId(...)
{
    return Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...))
        .Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
        //etc.
        ;
}
Run Code Online (Sandbox Code Playgroud)

和库会照顾短路,等我还添加了从隐式转换Select<T>T的,所以我可以写

public Guid GetPropertyId(...)
{
    ServiceResult result = Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...));

    return result.Id;
}
Run Code Online (Sandbox Code Playgroud)

我真正希望能够做的是在没有赋值的情况下隐式转换为T:

public Guid GetPropertyId(...)
{
    return 
        //This is the part that I want to be implicitly cast to a ServiceResult
        Select
        .Either(TryToGetTheId(...))
        .Or(TrySomethingElseToGetTheId(...))
        //Then I want to access this property on the result of the cast
        .Id;
}
Run Code Online (Sandbox Code Playgroud)

但是,指定的语法不起作用 - 我必须将其分配给变量,或者显式地转换它.有没有办法获得内联隐式转换?

编辑 …

c# casting implicit

4
推荐指数
2
解决办法
1739
查看次数

标签 统计

c# ×1

casting ×1

implicit ×1