mardi 23 juillet 2019

How to implement simple generic nesting in C#?

I am nesting multiple generic interfaces together, like

interface A<T1> where T1 : Parent
{
    T1 fun();
}

class B : A<Child>
{
    public Child fun()
    {
        ...
    }
}

interface C<T1, T2> where T2 : A<T1> where  T1 : Parent
{
    T1 fun();
}

class D : C<Child, B>
{
    private B b;

    public Child fun()
    {
        return b.fun();
    }
}

Child is a subclass of Parent.

in this way, the code are so redundant and ugly.

I want to get a simple implementation, like

interface A<T1> where T1 : Parent
{
    T1 fun();
}

class B : A<Child>
{
    public Child fun()
    {
        ...
    }
}

interface C<T2> where T2 : A<?>
{
    ? fun();
}

class D : C<B>
{
    private B b;

    public Child fun()
    {
        return b.fun();
    }
}

Is there any way?

Aucun commentaire:

Enregistrer un commentaire