Re: Strange Static contstructor behavior



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
.



Relevant Pages

  • Re: Strange Static contstructor behavior
    ... these static variables that are populated by the parent. ... Then you only want one instance of the parent class. ... The static constructor still fires twice. ... generic class is created on your behalf. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using a Resource as a Class Property
    ... if you don't define a __constructmethod in the child ... the parent class' constructor will get called automatically. ... In OOP the constructor of the grandest child:) will be called when a class is instantiated as an object. ...
    (comp.lang.php)
  • Re: accessing protected data members of instance of parent class
    ... > | them without accessor methods. ... > | properly copies those data members. ... > You haven't said wheter the Parent class had an accessible ... the parent class does not have a copy constructor -- or even an ...
    (comp.lang.cpp)
  • Memory Space Allocation and subclasses
    ... Now because this is a subclass, the parent ... constructor is called as the first line in the constructor block, ... parent constructor ONLY step 2 and 3 kick in for the parent class. ... member variables) cannot take place without step one (Memory space ...
    (comp.lang.java.programmer)
  • C++/CLI generic constructor constraint
    ... I am trying to write a generic class that instantiates the generic ... but I can not find the correct way to give it the constructor ... Prev by Date: ...
    (microsoft.public.dotnet.languages.vc)

Loading