2017-01-19 39 views
2

我是C#的新手(我是Java開發人員)並且對泛型有個疑問。我有一個嵌套類泛型類:我該如何擴展嵌套在另一個泛型類中的類?

public class FlowChartBuilder<TEntity, TLink> 
    where TEntity : FlowChartBuilder<TEntity, TLink>.Entity 
    where TLink : FlowChartBuilder<TEntity, TLink>.Link 
{ 
    public abstract class Link { } 
    public abstract class Entity { } 
} 

下一個我試圖擴展這些類:

public class ChartEntity<T>: FlowChartBuilder<ChartEntity<T>, ChartLink>.Entity 
{ 
} 

public class ChartEntity<T>: FlowChartBuilder<ChartEntity<T>, ChartLink<ChartEntity<T>>>.Entity 
{ 
} 

但我得到一個錯誤:

"TEntity" type can't be used like a parameter of "TEntity" type in the universal type or method "FlowChartBuilder". There isn't a transformation-packaging or a transformation of a type parameter from the "TEntity" to the "PM.Utils.Diagram.FlowChartBuilder>.Entity".

怎麼寫正確?

+2

將是冷靜,如果你可以用英語發表您的錯誤 –

+1

對不起,我剛纔沒有注意到。我已經翻譯過了。 –

+1

我認爲這是一個重複的這個http://stackoverflow.com/questions/21566701/nested-class-that-inherits-from-its-generic-parent-class可能你會發現你的答案。它告訴用繼續代替繼承。 –

回答

2

你試圖做的事情是不可能的。改爲使用「繼承」。

檢查這裏的解決方法,Nested class that inherits from its generic parent class

+0

對不起,我有一個混蛋。它不適合我的情況。或者我不好。 –

+0

你能否提供用例?你將無法繼承這些課程,所以請告訴我們爲什麼你想這樣做。 –

+0

我可以訪問這些嵌套類的私有成員。也許,C#有另一種方式,只是我不知道。 –

相關問題