Can not see the Form in the Designer. "The type initializer threw an exception." error
I got a problem recently when one of the forms I created could not be opened in Designer. Error message was as brief as "The type initializer for "..." threw an exception.". I found a good advice from Microsoft Developer Support guy Keith Fink how to debug the problem here.
Here is the advice:
Usually this means that your constructor of a particular type is throwing an exception. One way to debug this is to use aseparate instance of Devenv.exe and attach to the instance where yourproject is open, and then open the inherited form that causes the problem.
In the instance of Devenv.exe that you are using to debug with, followthese steps.
1. Start Devenv.exe.2. Select Tools->Debug Processes.3. Select the other instance of Devenv (where your project is loaded), andclick Attach.4. Make sure to select Common Language Runtime, and uncheck Nativedebugging, and click OK.5. Click Close.6. Select Debug->Exceptions.7. Expand Common Language Runtime Exceptions->System.8. Select the System.TypeInitializationException.9. Under the "When the exception is thrown" section, select the "Break intothe debugger" option, and click OK.10. Switch over to the other instance of Devenv, and open the form so thatthe exception is thrown. When that happens the first instance of Devenvshould break on the line of code where the exception is thrown.
One more hint - in the Form code DesignMode property is useful (it'll tell you if your Form is loaded in designer).
Note that the DesignMode property should not be checked from within the constructor or from any code that the constructor calls. A constructor is called before a control is sited, and it's the site that determines whether or not a control is in design mode. DesignMode will also be false in the constructor. I personally like to put it into Form_Load event (Form_FormClosing if you want to do something like logging on form close).
Example:
...
if(!this.DesignMode)
{
// chek authentication or something else
}
And finally a good article about Design-time integration of controls.
I got a problem recently when one of the forms I created could not be opened in Designer. Error message was as brief as "The type initializer for "..." threw an exception.". I found a good advice from Microsoft Developer Support guy Keith Fink how to debug the problem here.
Here is the advice:
Usually this means that your constructor of a particular type is throwing an exception. One way to debug this is to use aseparate instance of Devenv.exe and attach to the instance where yourproject is open, and then open the inherited form that causes the problem.
In the instance of Devenv.exe that you are using to debug with, followthese steps.
1. Start Devenv.exe.2. Select Tools->Debug Processes.3. Select the other instance of Devenv (where your project is loaded), andclick Attach.4. Make sure to select Common Language Runtime, and uncheck Nativedebugging, and click OK.5. Click Close.6. Select Debug->Exceptions.7. Expand Common Language Runtime Exceptions->System.8. Select the System.TypeInitializationException.9. Under the "When the exception is thrown" section, select the "Break intothe debugger" option, and click OK.10. Switch over to the other instance of Devenv, and open the form so thatthe exception is thrown. When that happens the first instance of Devenvshould break on the line of code where the exception is thrown.
One more hint - in the Form code DesignMode property is useful (it'll tell you if your Form is loaded in designer).
Note that the DesignMode property should not be checked from within the constructor or from any code that the constructor calls. A constructor is called before a control is sited, and it's the site that determines whether or not a control is in design mode. DesignMode will also be false in the constructor. I personally like to put it into Form_Load event (Form_FormClosing if you want to do something like logging on form close).
Example:
...
if(!this.DesignMode)
{
// chek authentication or something else
}
And finally a good article about Design-time integration of controls.
No comments:
Post a Comment