C#曖昧な通話を異なる戻り値の型
-
18-09-2019 - |
質問
私は2つの拡張方法に変換するPython文書体/オブジェクトです。
public static ProductTemplate Convert(this Document document)
{
return null;
}
public static Product Convert(this Document document)
{
return null;
}
ことが予想される曖昧なコ誤ったのがどんな風に思修理が可能です。
優しいあいさつ
Pickels
解決
関数は、戻り値の型によってオーバーロードすることはできません。あなたは多分に自分の関数の名前を変更する必要があります。
ConvertToProductTemplate() そして ConvertToProduct()
または共通ベースクラスまたはインタフェースを返す一つの関数にそれらを回します。 (しかし、その後、発信者は、彼らがあなたの結果を得るとき、キャストを行う必要があります)。
他のヒント
あなたはConvert
方法は、一般的な作ることができます:
public static T ConvertTo<T>(this Document doc) where T : SomeBaseClassOrInterface
{
return null;
}
そして、そのようにそれを使用します:
var document = new Document();
var temp = document.ConvertTo<ProductTemplate>(); // returns a ProductTemplate
var prod = document.ConvertTo<Product>(); // returns a Product
あなたは名を変更することができます:
public static ProductTemplate ConvertToProductTemplate(this Document document)
{
return null;
}
public static Product ConvertToProduct(this Document document)
{
return null;
}
っているような気がする 製品 や ProductTemplate 授業関連など 製品 広 ProductTemplate).について知っておきましょ右したりすることも可能ですの基底クラス(ProductTemplate この場合).
トーマス-Lyckenれ一般法である私の考えではなかなか良いアイデアがあるための共通インターフェース製品productTemplateしたりすることも可能ですいインタフェースの代わりに 製品 や ProductTemplate.
例によるトーマス-Lycken):
public static T ConvertTo<T>(this Document doc) where T : SomeBaseClassOrInterface
{
return null;
}
例(はい):
public static SomeBaseClassOrInterface ConvertTo(this Document doc)
{
return null;
}
とであれば、一般的なインターフェースを組み合わせて管理したい新規作成していただく変更名:)
所属していません StackOverflow