Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
20 Feb 2010 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here