How to resolve TypeInitializationException?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • K Viltersten

    #1

    How to resolve TypeInitializationException?

    I'm getting TypeInitilizati on error and
    according to

    it happens when an error occurs in a static
    constructor.

    The problem is that in my static class, i
    have no constructors declared. Any general
    hints on how to approach this? Frankly, i
    don't even know what code to present!


    --
    Regards
    Konrad Viltersten
    ----------------------------------------
    May all spammers die an agonizing death;
    have no burial places; their souls be
    chased by demons in Gehenna from one room
    to another for all eternity and beyond.


  • Marc Gravell

    #2
    Re: How to resolve TypeInitializat ionException?

    * 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

    Comment

    • K Viltersten

      #3
      Re: How to resolve TypeInitializat ionException?

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


      --
      Regards
      Konrad Viltersten
      ----------------------------------------
      May all spammers die an agonizing death;
      have no burial places; their souls be
      chased by demons in Gehenna from one room
      to another for all eternity and beyond.


      Comment

      • Marc Gravell

        #4
        Re: How to resolve TypeInitializat ionException?

        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

        Comment

        • K Viltersten

          #5
          Re: How to resolve TypeInitializat ionException?

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

          --
          Regards
          Konrad Viltersten
          ----------------------------------------
          May all spammers die an agonizing death;
          have no burial places; their souls be
          chased by demons in Gehenna from one room
          to another for all eternity and beyond.


          Comment

          • Marc Gravell

            #6
            Re: How to resolve TypeInitializat ionException?

            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

            Comment

            • Marc Gravell

              #7
              Re: How to resolve TypeInitializat ionException?

              (I missed a "y" in the last - assume it was there ;-p)

              Comment

              • K Viltersten

                #8
                Re: How to resolve TypeInitializat ionException?

                >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;
                }
                }
                Well, one learns something new every day.
                This was the wisdom for today.

                Thanks a lot for the clear illustration.

                --
                Regards
                Konrad Viltersten
                ----------------------------------------
                May all spammers die an agonizing death;
                have no burial places; their souls be
                chased by demons in Gehenna from one room
                to another for all eternity and beyond.


                Comment

                Working...