Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Prevent code executing at Design Time in Visual Studio for Winforms Apps

, 20 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Either method is good for testing if you are in the designer, but another problem aside from preventing code execution, is preventing code from being jitted in the Designer. For example, you may reference assemblies or components that are dependent on native code, which cannot be loaded into any...
Either method is good for testing if you are in the designer, but another problem aside from preventing code execution, is preventing code from being jitted in the Designer. For example, you may reference assemblies or components that are dependent on native code, which cannot be loaded into any process (most likely because it may be dependent on a specific process, as one might find in a plug-in architecture or CLR host).
 
The problem is that Visual Studio will 'jit' all code that is called from the constructor of a designable component when it is opened in the designer. This leads Visual Studio to load any assembly that contains a type that is referenced from the jit'ed code.
 
If for some reason, a type cannot be loaded, it usually leads to a WSOD and the designable component cannot be opened in the designer.
 
The way to stop Visual Studio from trying to load 'unloadable' code at design time, is to get that code out of the constructor or any method that's called from the constructor, and instead place it in a seperate method, and then invoke the method from the constructor <i><b>via reflection</b></i>.
 
Visual Studio will jit code in any method that is called from a constructor explicitly, but will not try to jit code that is called from a constructor indirectly via reflection.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tonyt

United States United States
No Biography provided

Comments and Discussions

 
GeneralI'm using a BaseForm from which every other form inherits, b... PinmemberShago10-Jun-10 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 21 Feb 2010
Article Copyright 2010 by tonyt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid