Click here to Skip to main content
15,883,941 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
20 Feb 2010CPOL1 min read 9.3K   1   1
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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