Re: Static property of type parameter - is this a CLR or C# limitation?




Barry Kelly <barry.j.kelly@xxxxxxxxx> wrote:

[...some insights in generics compilation...]

So, I would say it is a current architectural limitation of the CLR.

Thanks for a detailed explanation.

I'll go for a combination of reflection and static fields. Using
reflection in the class initializer will be faster than cluttering
the constructors with factory parameters, I think.

My SharedPetLover<T> gets a static field that is filled in by
using reflection to get the SharedPet<T> singleton. Or,
expanding on your version:

class A<T>
{
public static T Value
{
get { return default(T); }
}
}

class B<T,U>
where T : A<U>
{
public U Value
{
get { return value; }
}
private static U value =
(U)typeof(U).InvokeMember(
"Value", flags, null, null, null);
const flags =
BindingFlags.GetProperty
| BindingFlags.Public
| BindingFlags.Static
| BindingFlags.FlattenHierarchy;
}


This may complicate the initialization process a bit,
but once it's done I get fast object creation. (I'm
still slightly puzzled that it can't be done without
reflection or factories, but I'll take your words for
that.)

Regards/Ole N.


.