How to resolve TypeInitializationException?

  • Thread starter Thread starter K Viltersten
  • Start date Start date
* Are you using any reflection (i.e. MakeGenericType)? Or just regular
C#?
* Does your static class have any static field initializers that might
be breaking? i.e.

class Foo {
static Bar sharedBar = new Bar();
}

Such initializers are essentially part of the constructor (and are
actually implemented as such by the current compiler), and will cause
the same if they fail.

Marc
 
* Does your static class have any static
field initializers that might be breaking?
i.e.
class Foo {
static Bar sharedBar = new Bar();
}

Such initializers are essentially part of
the constructor...

I didn't know that. And yes, i do have that.
What might be a good resolution, generally
speaking? I'd go with the following.

class Foo {
static Bar sharedBar;// = new Bar();
static Foo() {
Foo.sharedBar = new Bar();
}
}

What do you think?
 
What do you think?

That will still break in exactly the same way, but at least it gives
you the opportunity to try/catch the exception and do something else
(or just debug/log the actual problem).

Marc
 
What do you think?
That will still break in exactly the same
way, but at least it gives you the
opportunity to try/catch the exception
and do something else (or just debug/log
the actual problem).

Exactly. That's what i ment. Thanks!

On a similar note. If i initialize
non-static members at the declaration point
instead of the constructor, are those
stetements also automagically put in the
empty constructor, the same way as you
described to be the case for static ones?
 
On a similar note. If i initialize
non-static members at the declaration point
instead of the constructor, are those
stetements also automagically put in the
empty constructor, the same way as you
described to be the case for static ones?

Yes. Note that they preceed the "base" call - i.e.

class Foo {
public int x = 5, y;
public Foo() : base() { y = 10;}
}

compiles to the IL for (non-legal pseudo-code):

class Foo {
public int x;
public Foo() {
x = 5;
base();
y = 10;
}
}

Marc
 
On a similar note. If i initialize
non-static members at the declaration point
instead of the constructor, are those
stetements also automagically put in the
empty constructor, the same way as you
described to be the case for static ones?

The compiled constructor for C# consists of the following pieces, in
order:

o Variable initializers (only if chaining to a base constructor?)
o Chaining to a base constructor or other "this" constructor
o The constructor body

So it's not quite like putting the initializers into the body, as they
will be executed before the base class constructor.

Jon
 
On a similar note. If i initialize
Yes. Note that they preceed the "base" call - i.e.

class Foo {
public int x = 5, y;
public Foo() : base() { y = 10;}
}

compiles to the IL for (non-legal pseudo-code):

class Foo {
public int x;
public Foo() {
x = 5;
base();
y = 10;
}
}

Well, one learns something new every day.
This was the wisdom for today.

Thanks a lot for the clear illustration.
 
Back
Top