How to resolve TypeInitializationException?

M

Marc Gravell

* 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
 
K

K Viltersten

* 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?
 
M

Marc Gravell

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
 
K

K Viltersten

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?
 
M

Marc Gravell

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
 
J

Jon Skeet [C# MVP]

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
 
K

K Viltersten

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.
 

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