標題道歉 - 我真的想不出一個很好的方式來描述我的要求。使用類型定義在通用類型
我希望能夠定義一個通用的接口(或類,沒關係),從而Type參數提供另一個可以從類訪問的類型。希望這段代碼能夠解釋。
interface IFoo
{
TOtherType DependentType { get; }
}
interface IMyGeneric<TBar> where TBar : IFoo
{
TBar ReturnSomeTBar();
TBar.DependentType ReturnSomeTypeOnTBar();
}
因此,在這個例子中,我希望有一個類來實現IFoo
(如Foo
)和揭露另一種類型,DependentType(例如int
),所以我可以用IMyGeneric<Foo>
有方法:
public Foo ReturnSomeTBar()
{
}
public int ReturnSomeTypeOnTBar()
{
}
顯然,上面的代碼不會編譯,所以有沒有辦法實現這種通用鏈接行爲?
事實上,2型參數是我考慮過的一種解決方法,但它不是我所需要的行爲,因爲這些類型將是獨立的。這將是一個可以接受的妥協 – RichK