2

I'm new to C# (I am a Java developer) and have a question about generics. I have a generic class with nested classes:

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 { }
}

The next I try to extends these classes:

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

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

But I get an error:

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

How to write it correct?

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
Denis Sologub
  • 7,277
  • 11
  • 56
  • 123

1 Answers1

2

What you are trying to do is impossible. Use Composition over inheritance instead.

Check here for a workaround Nested class that inherits from its generic parent class

Community
  • 1
  • 1
Bruno Belmondo
  • 2,299
  • 8
  • 18