Re: Strange Static contstructor behavior



Thanks a lot. That does make sense. I will give that a try.

"Peter Duniho" wrote:

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
    ... I want the derived classes ... Then you only want one instance of the parent class. ... The static constructor still fires twice. ... I think instead you would want to create a non-generic base class that is composited into the generic class rather than being inherited. ...
    (microsoft.public.dotnet.languages.csharp)
  • 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)
  • Re: C# 2.0: new() constraint on constructors having parameters?
    ... I've created a generic class having a type argument where I want to use ... I'm calling these constructors ... actually *don't* have a parameterless constructor. ...
    (microsoft.public.dotnet.languages.csharp)
  • Referncing generic in XML comments
    ... A have a generic class - InfoCollection ... The code comment for my constructor is: ... I get a warning in the compilation saying that the comment has syntactically ...
    (microsoft.public.dotnet.framework)
  • 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)

Loading