2017-01-19 38 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#有另一种方式,只是我不知道。 –

相关问题