gpt4 book ai didi

c# - 如何从嵌套在另一个泛型类中的类扩展我的类?

转载 作者:行者123 更新时间:2023-11-30 23:17:03 25 4
gpt4 key购买 nike

我是 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".

怎么写才正确?

最佳答案

你想做的事是不可能的。改用组合而不是继承。

在此处查看解决方法 Nested class that inherits from its generic parent class

关于c# - 如何从嵌套在另一个泛型类中的类扩展我的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com