Re: Strange Static contstructor behavior
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 28 Jan 2008 10:45:04 -0800
On Mon, 28 Jan 2008 09:16:00 -0800, chriscap <chriscap@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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
.
- Follow-Ups:
- Re: Strange Static contstructor behavior
- From: chriscap
- Re: Strange Static contstructor behavior
- References:
- Strange Static contstructor behavior
- From: chriscap
- Strange Static contstructor behavior
- Prev by Date: Re: General exception Handling question
- Next by Date: Re: Properties Question
- Previous by thread: Strange Static contstructor behavior
- Next by thread: Re: Strange Static contstructor behavior
- Index(es):
Relevant Pages
|
Loading