保留的问题 - 请参阅底部的编辑
我正在开发一个小型函数库,基本上是通过隐藏基本的圈复杂度来提供一些可读性。提供者称为Select<T>
(有一个名为的辅助工厂Select
),用法类似于
public Guid? GetPropertyId(...)
{
return Select
.Either(TryToGetTheId(...))
.Or(TrySomethingElseToGetTheId(...))
.Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
//etc.
;
}
并且库将处理短路等问题。我还添加了一个隐式转换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;
}
但是,指定的语法不起作用 - 我必须将其分配给变量,或显式转换它。有没有办法获得隐式强制转换?
EDIT
我想做的是这样的:
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);
虽然隐式强制转换在这里不起作用,但您可以通过添加一个比显式强制转换更好的方法Value
财产给Select<T>
。那么你的表情会是Select.[operations].Value.Id
,读起来仍然相当不错。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)