C
chriscap
I am having some unexepected results with a static constructor. I have the
following class design
ParentClass<TType>
{
static ParentClass() { doStuff();
}
ChildClassA<SpecificTypeA>
{
}
ChildClassB<SpecificTypeB>
{
}
To sum up, I have a generic parent class with a static constructor. This
constructor populates some static variables. I want the derived classes
(which derive the parent class with specific types) to take advantage of
these static variables that are populated by the parent.
I would expect the static constructor to run once. However, it is running
twice, once for each derived class. I suspect that it might even run a third
time if I used the parent class directly. I've tried just using the parent
class and passing in the specific type instead of using the inheritied
classes. The static constructor still fires twice.
Do generic classes not share static variables? I suppose this might make
sense since the initialization of static member variables may be based on the
generic type, hence they couldn't be shared. Can anyone point me to a
microsoft reference that describes this behavior or validate what I'm seeing?
I've looked all over on MSDN and can't find anything on how static variables
work with a generic type
following class design
ParentClass<TType>
{
static ParentClass() { doStuff();
}
ChildClassA<SpecificTypeA>
{
}
ChildClassB<SpecificTypeB>
{
}
To sum up, I have a generic parent class with a static constructor. This
constructor populates some static variables. I want the derived classes
(which derive the parent class with specific types) to take advantage of
these static variables that are populated by the parent.
I would expect the static constructor to run once. However, it is running
twice, once for each derived class. I suspect that it might even run a third
time if I used the parent class directly. I've tried just using the parent
class and passing in the specific type instead of using the inheritied
classes. The static constructor still fires twice.
Do generic classes not share static variables? I suppose this might make
sense since the initialization of static member variables may be based on the
generic type, hence they couldn't be shared. Can anyone point me to a
microsoft reference that describes this behavior or validate what I'm seeing?
I've looked all over on MSDN and can't find anything on how static variables
work with a generic type