Static property of type parameter - is this a CLR or C# limitation?
- From: "Ole Nielsby" <ole.nielsby@xxxxxxxxxxxx>
- Date: Sun, 14 May 2006 20:12:40 +0200
Here comes a generic class with a static property.
Let's say they are exotic singleton pets.
abstract class Pet {...}
abstract class SharedPet<T>: Pet
where T: SharedPet<T>, new()
{
private static T singleton = new T();
public T Singleton {get {return singleton;}}
}
class KingKong : SharedPet<KingKong> {...}
class Gozilla : SharedPet<Gozilla> {...}
I use pets as a parameter to another generic class:
class PetLover<T>
where T : Pet, new()
{
public virtual T GetAPet
{ return new T(); } //Let it rain cats and dogs...
}
class SharedPetLover<T> : PetLover<T>
where T : SharedPet<T>, new()
{
public override T GetAPet //...but not gozillas
{ return T.Singleton; }
// Compile time error:
// 'T' is a 'type parameter', which is not
// valid in the given context'
}
Unforunately, it seems I can't access a static member
of T, though the constraint ensures it is there.
Is this a restriction of C#, or is it a CLR thing?
Is there a neat way of getting around it, or am I left
with the options of either rewriting the GetAPet
override using reflection, or doing copy-paste
overrides in KingKongLover, GozillaLover,
KermitLover, ReptarLover...?
(In case anyone wonders what the "real beasts"
look like: I'm trying to do a continuation passing
style implementation of a programming language
with a DOM like data model, and the nodes used
to implement continuations etc. must be as slim
as possible, yet compliant with the data model.)
.
- Follow-Ups:
- Re: Static property of type parameter - is this a CLR or C# limitation?
- From: Barry Kelly
- Re: Static property of type parameter - is this a CLR or C# limitation?
- Prev by Date: SortedList<> and SortedDictionary<>
- Next by Date: Re: SortedList<> and SortedDictionary<>
- Previous by thread: SortedList<> and SortedDictionary<>
- Next by thread: Re: Static property of type parameter - is this a CLR or C# limitation?
- Index(es):