保存された質問 - 下部の編集を参照してください
。基本的に、基本的な循環的複雑さを隠すことで読みやすさを提供するために、小さな機能ライブラリに取り組んでいます。プロバイダーが呼び出されSelect<T>
( と呼ばれるヘルパー ファクトリSelect
を使用)、使用法は次のようになります。
public Guid? GetPropertyId(...)
{
return Select
.Either(TryToGetTheId(...))
.Or(TrySomethingElseToGetTheId(...))
.Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...))
//etc.
;
}
ライブラリが短絡などを処理します。 からSelect<T>
への暗黙的な変換も追加した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);