Given the current structure of generic classes.
public abstract class Foo<TFoo, TBar>
where TFoo : Foo<TFoo, TBar>
where TBar : Bar<TFoo, TBar>
{
}
public abstract class Foo<TFoo> : Foo<TFoo, BarImpl>
where TFoo : Foo<TFoo>
{
}
public class FooImpl : Foo<FooImpl>
{
}
public abstract class Bar<TFoo, TBar>
where TFoo : Foo<TFoo, TBar>
where TBar : Bar<TFoo, TBar>
{
}
public abstract class Bar<TFoo> : Bar<TFoo, BarImpl>
where TFoo : Foo<TFoo>
{
}
public class BarImpl : Bar<FooImpl>
{
}
What I want is to set a default Bar
on each implementation of Foo<TFoo>
.
At some other part in the code an instance of TBar
is created which fails in case it is Bar<TFoo>
as this is an abstract
class.
However, the following error is thrown and I don't get the point what I can do or if this even possible at all.
The type 'BarImpl' must be convertible to 'Bar' in order to use it as parameter 'TBar' in the generic class 'Foo'
I already tried to let BarImpl
derive from Bar<FooImpl, BarImpl>
which had no effect.
Changing it to
public abstract class Foo<TFoo> : Foo<TFoo, Bar<TFoo>>
where TFoo : Foo<TFoo>
{
}
public abstract class Bar<TFoo> : Bar<TFoo, Bar<TFoo>>
where TFoo : Foo<TFoo>
{
}
will work until the object of type Bar<TFoo>
is intantiated (because of it's abtract).