Generic contraints on derived classes
Asked Answered
G

3

8

I have class A:

public class ClassA<T>

Class B derives from A:

public class ClassB : ClassA<ClassB>

Class C derives from class B:

public class ClassC : ClassB

Now I have a generic method with constraints

public static T Method<T>() where T : ClassA<T>

OK, now I want to call:

ClassC c = Method<ClassC>();

but I get the compile error saying: Type argument 'ClassC' does not inherit from or implement the constraint type 'ClassA<ClassC>.

Yet, the compiler will allow:

ClassB b = Method<ClassB>();

My understanding is that this fails because ClassC inherits ClassA<ClassB> instead of ClassA<ClassC>

My real question is, is it possible to create a class deriving from ClassB that can be used in some way with the generic method?

This may seem like generics are overused and I would agree. I am trying to create business layer objects deriving from the subsonic data objects in a separate project.

Note: I have put the < T > with extra spaces otherwise they get stripped from the question.

Geniagenial answered 24/10, 2008 at 12:24 Comment(0)
V
7

Well, you could change Method to:

public static T Method<T,U>() where T : ClassA<U> where U : T

Does that help at all? It's not much use if you can't change Method of course...

Vue answered 24/10, 2008 at 12:29 Comment(1)
Yep, this does solve the problem. The method is actually: DB.Get< T >(string primaryColumnValue) T used to return a data object from the database. It feels like it is getting more complicated that it needs to be...I need to step back and rethink about the architecture.Geniagenial
I
2

No. You must change or wrap this method.

Here is the reason.

ClassC inherits from ClassB which inherits from ClassA(ClassB)

ClassC does not inherit from ClassA(ClassC)

No child of ClassB will inherit from ClassA(child class), because they instead inherit from ClassB and ClassB does not inherit from ClassA(child class).

Generic types are invariant.

Implied answered 24/10, 2008 at 12:39 Comment(0)
N
1

In most cases it is possible to solve this scenario by having a base non-generic abstract class:

public abstract class BasicClassA
{
}

public class ClassA<T> : BasicClassA
{
}

public class ClassB : ClassA<ClassB>
{
}

public class ClassC : ClassB
{
}

public static T Method<T>() where T : BasicClassA
{
    return null;
}

Your mileage may vary, though.

Narghile answered 24/10, 2008 at 12:59 Comment(1)
In this case this suggestion doesn't solve the scenario because within "Method" methods are called on ClassA<T> that return type T. Thanks for the suggestion though.Geniagenial

© 2022 - 2024 — McMap. All rights reserved.