保留的问题 - 请参阅底部的编辑
我正在开发一个小型功能库,主要是通过隐藏基本的圈复杂性来提供一些可读性。调用提供者Select<T>
(带有一个名为 的辅助工厂Select
),用法类似于
public Guid? GetPropertyId(...)
{
return Select
.Either(TryToGetTheId(...))
.Or(TrySomethingElseToGetTheId(...))
.Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
//etc.
;
}
并且库会处理短路等问题。我还添加了一个隐式转换 from Select<T>
to T
,所以我可以写
public Guid GetPropertyId(...)
{
ServiceResult result = Select
.Either(TryToGetTheId(...))
.Or(TrySomethingElseToGetTheId(...));
return result.Id;
}
我真正想做的是在没有赋值的情况下隐式转换为 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;
}
但是,指定的语法不起作用 - 我必须将其分配给变量,或显式转换它。有没有办法获得隐式转换内联?
编辑
我想做的是:
class Foo {
public int Fuh { get; set; }
}
class Bar {
private Foo _foo;
public static implicit operator Foo (Bar bar)
{
return bar._foo;
}
}
//What I have to do
Foo bar = GetABar();
DoSomethingWith(bar.Fuh);
//What I want to do
DoSomethingWith(GetABar().Fuh);