child type cannot be used as generic type parameter

A

Andrus

How to fix ?

Andrus.

code to reproduce:

namespace test {

class ModelGenericBase<T> { }
class GenericBaseForm<T> where T : ModelGenericBase<T> { }

class KlientEntity : ModelGenericBase<KlientEntity> { }
class KlientBase : KlientEntity { }


class test {
void tesxt() {
//Error 1 The type 'test.KlientBase' must be convertible to
// 'test.ModelGenericBase<test.KlientBase>' in order to use it as
// parameter 'T' in the generic type or method
// 'test.GenericBaseForm<T>'
GenericBaseForm<KlientBase> x;
}
}
}
 
J

Jon Skeet [C# MVP]

Andrus said:
How to fix ?

Well, it's not clear exactly what you're trying to do. It's clear why
your code won't work though - the constraint specifies that T must
derive from ModelGenericBase<T>. Now you're trying to use KlientBase as
T, but KlientBase doesn't derive from ModelGenericBase<KlientBase> it
derives (indirectly) from ModelGenericBase<KlientEntity>.
 
A

Andrus

KlientBase doesn't derive from ModelGenericBase said:
it derives (indirectly) from ModelGenericBase<KlientEntity>.

Surprising.
KlientEntity derives from KlientBase
So ModelGenericBase<KlientEntity> derives also from
ModelGenericBase<KlientBase>
 
P

Peter Duniho

Andrus said:
Surprising.
KlientEntity derives from KlientBase
So ModelGenericBase<KlientEntity> derives also from
ModelGenericBase<KlientBase>

Actually, while that's not an uncommon interpretation, it's not true.

If it were, you could wind up with situations in which your
ModelGenericBase<KlientEntity> class was using objects that only derived
from KlientBase and not KlientEntity.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top