How to implement a generic interface with a child generic interface

Kyle Moyer

I'm having an issue with implementing parent/child interfaces when both of them are generic. The best answer I've been able to find is that it isn't possible, but I also haven't been able to find anyone else asking the exact same question. I'm hoping that I just don't know the right syntax to make the compiler understand what I'm trying to do. Here is a stripped down example of the code I'm trying to implement.

public interface I_Group<T>
    where T : I_Segment<I_Complex>
{
    T Segment { get; set; }
}

public interface I_Segment<T>
    where T : I_Complex
{
    T Complex { get; set; }
}

public interface I_Complex
{
    string SomeString { get; set; }
}

public partial class Group : I_Group<Segment>
{   
    private Segment segmentField;

    public Group() {
        this.segmentField = new Segment();
    }

    public Segment Segment {
        get {
            return this.segmentField;
        }
        set {
            this.segmentField = value;
        }
    }
}

public partial class Segment : I_Segment<Complex> {

    private Complex complexField;

    public Segment() {
        this.complexField = new Complex();
    }

    public Complex Complex {
        get {
            return this.c_C001Field;
        }
        set {
            this.c_C001Field = value;
        }
    }
}

public partial class Complex : I_Complex {

    private string someStringField;

    public string SomeString {
        get {
            return this.someStringField;
        }
        set {
            this.someStringField = value;
        }
    }
}

So here, Complex is the grandchild, which implements I_Complex without error. Segment is its parent, which implements I_Segment without error. The issue is with the grandparent, Group, trying to implement I_Group. I get the error

The type 'Segment' cannot be used as type parameter 'T' in the generic type or method 'I_Group<T>'. There is no implicit reference conversion from 'Segment' to 'I_Segment<I_Complex>'.

I am led to believe this is an issue with covariance, but I was also led to believe this was something that was supposed to work in C# 4.0. This works when the child isn't generic, which leads me to think that there must exist some syntax to get this to compile properly. Am I doing something wrong? Is this even possible? And if not, could someone help me understand why not?

MarcinJuraszek

You can add second generic type parameter into I_Group interface declaration:

public interface I_Group<T, S>
    where T : I_Segment<S>
    where S : I_Complex
{
    T Segment { get; set; }
}

And specify explicitly both types in Group class declaration:

public partial class Group : I_Group<Segment, Complex>

It will make your code compile.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

implement generic generic interface in scala

From Dev

Implement Interface Generic Function

From Dev

Implement a generic interface

From Dev

Implement Interface Generic Function

From Dev

How do I implement a generic class that has an constraint for a generic interface?

From Dev

How to implement a generic interface which implements another interface?

From Dev

Dictionary of classes that implement a generic interface

From Dev

Dictionary of classes that implement a generic interface

From Dev

How to overload an Interface with a generic Interface

From Dev

How to reuse generic interface?

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

Implement a generic C++/CLI interface in a C# generic interface

From Dev

Java how to implement interface with a variadic method and generic return type

From Dev

How can I implement a generic interface in Rhino JS?

From Dev

How to just get the method in the implement class with a generic interface in Java

From Dev

Generic interface parameters in a generic interface

From Dev

Generic Interface, of Generic Interface with Generics

From Dev

Generic interface parameters in a generic interface

From Dev

Using Reflection.Emit to implement generic interface

From Dev

Abstract class implement a generic Interface Type

From Dev

Why can't a Java Generic implement an Interface?

From Dev

Implement interface with enum value as generic parameter

From Dev

C# Factory of classes that implement generic Interface

From Dev

Typescript Can't implement generic function interface

Related Related

  1. 1

    How to implement a generic interface with a child generic interface

  2. 2

    How to implement an interface with generic types?

  3. 3

    How implement an interface Generic Method

  4. 4

    How to implement an interface with generic types?

  5. 5

    How implement an interface Generic Method

  6. 6

    implement generic generic interface in scala

  7. 7

    Implement Interface Generic Function

  8. 8

    Implement a generic interface

  9. 9

    Implement Interface Generic Function

  10. 10

    How do I implement a generic class that has an constraint for a generic interface?

  11. 11

    How to implement a generic interface which implements another interface?

  12. 12

    Dictionary of classes that implement a generic interface

  13. 13

    Dictionary of classes that implement a generic interface

  14. 14

    How to overload an Interface with a generic Interface

  15. 15

    How to reuse generic interface?

  16. 16

    Implement a generic C++/CLI interface in a C# generic interface

  17. 17

    Implement a generic C++/CLI interface in a C# generic interface

  18. 18

    Java how to implement interface with a variadic method and generic return type

  19. 19

    How can I implement a generic interface in Rhino JS?

  20. 20

    How to just get the method in the implement class with a generic interface in Java

  21. 21

    Generic interface parameters in a generic interface

  22. 22

    Generic Interface, of Generic Interface with Generics

  23. 23

    Generic interface parameters in a generic interface

  24. 24

    Using Reflection.Emit to implement generic interface

  25. 25

    Abstract class implement a generic Interface Type

  26. 26

    Why can't a Java Generic implement an Interface?

  27. 27

    Implement interface with enum value as generic parameter

  28. 28

    C# Factory of classes that implement generic Interface

  29. 29

    Typescript Can't implement generic function interface

HotTag

Archive