ptutt ptutt - 1 year ago 118
C# Question

Generic contraints on derived classes

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
instead of

My real question is, is it possible to create a class deriving from
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.

Answer Source

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