Strange Static contstructor behavior

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
 
P

Peter Duniho

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.

Then you only want one instance of the parent class. Generics isn't the
way to go here.
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.

Right. Every time you use the generic with a new type parameter, a new
generic class is created on your behalf. Each class that's created has
its own static constructor, which of course is then called.

I think instead you would want to create a non-generic base class that is
composited into the generic class (i.e. referenced explicitly when needed)
rather than being inherited. This way you have just the one class that's
used any time you need to get at the static fields.

For that matter, I think it's possible that if you made a non-generic base
class that your generic class inherited, you'd only get the static
constructor called once, since in that case the class being defined
multiply wouldn't be the class with the static constructor. (Sorry for
being wishy-washy...I can't actually test that theory at the moment,
otherwise I'd offer more specific information).

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