title: How best to debug "The type initializer for xx class threw an exception"
body: <p>I have one user reporting this exception.  My research here and elsewhere suggests this is a static contructor error.  The class concerned contains a number of static fields that are initialized.  I assume it is one of these but I am not sure how to find it.  The suggestion seems to be to include a try/catch block in the static contructor.  As written there is no explicit static contructor. </p>
  
  <p>I am thinking that I should write an explicit static constructor, move the initialization of the static fields into it and wrap them in the try/catch block.  I know I could try this but the error does not arise for me and I would like to send a modded executable to the user to try.  My application does log the errors so I should get a log from him if the exception is caught in the static constructor.</p>
  
  <p>The relevant (I hope) part of the class is here  (it is not actually my code but I can mod it as required):</p>
  
  <pre><code> [Serializable]
      public class PText : PNode, ISerializable {
  
      //comments elided by edit for brevity
          #region Fields
  
          public const int PROPERTY_CODE_FONT = 1 &lt;&lt; 18;
          public const int PROPERTY_CODE_TEXT = 1 &lt;&lt; 17;
          public static Font DEFAULT_FONT = new Font("Arial", 12);
          protected static readonly object PROPERTY_KEY_FONT = new object();
          protected static readonly object PROPERTY_KEY_TEXT = new object(); 
          private static Graphics GRAPHICS = Graphics.FromImage(new Bitmap(1, 1));
  
          [NonSerialized]
          private Color brushColor;
          private bool constrainHeightToTextHeight = true;
          private bool constrainWidthToTextWidth = true;
          private Font font;
  
          [NonSerialized]
          private Color penColor;
  
          [NonSerialized]
          private StringFormat stringFormat = new StringFormat();
          private String text;
  
          [NonSerialized]
          private Brush textBrush;
  
          #endregion Fields
  
          #region Constructors
          public PText() {
              textBrush = Brushes.Black;
          }
  
          public PText(String aText)
              : this() {
              Text = aText;
          }
  
          protected PText(SerializationInfo info, StreamingContext context)
              : base(info, context) {
              textBrush = PUtil.ReadBrush(info, "textbrush");
              TextAlignment = (StringAlignment)info.GetValue("alignment", typeof(int));
          }
  
          #endregion Constructors
  .......
  </code></pre>