Some time ago I published here a piece of code that detects the design mode in Visual Studio Designer. The code worked great (as long as you use only MS Visual Studio). However, after some time, with our project getting more and more complex, the solution turned out to be problematic, as its usage is limited only to classes that extend the BaseDesignModeUserControl. If, for instance, you need to extend System.ComponentModel.Component, then you have to duplicate the code (and the rhetorical question is who likes to duplicate the code?)
So, it turns out that we should forget about “the-base-class” approach and use an extension method instead:
public static class ExtensionMethods { private static bool? isDesignMode = null; [DebuggerStepThrough] public static bool IsDesignMode(this object obj) { if (!isDesignMode.HasValue) isDesignMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName.IndexOf("devenv") != -1); return isDesignMode.Value; } }
It seems that this piece of code may be called anywhere and I hope that this is the final solution of the problem.