C # Hide function from constructor

I have a function that throws an exception in the constructor. Can I avoid calling the function if the designer loads it. Is there an attribute or something that is better than try catch?

Additional information: I mean the designer of the visual studio for the forms of winning. My form uses a pointer that calls LoadProject () on initialization. Now I want to avoid the constructor calling the LoadProject () function.

+4
source share
3 answers

There are several ways to determine if you are in development mode:

  • Check the value of the DesignMode property of the control. This does not work in the constructor of the control, though, since it returns true only if the control was placed, which does not happen until the control is created. It also has an error that the user control inside the custom control always returns false
  • Check if the current application path contains devenv.exe using Application.ExecutablePath.ToLower().IndexOf("devenv.exe") . If so, the control is created by Visual Studio. A bit awful, but it works.
  • Check LicenseManager.UsageMode to LicenseUsageMode.Designtime (see my answer to Detecting design mode with the Controls constructor for more details). Note that this works in the constructor .

The wrapper of your function call in any of these checks should solve your problem.

+1
source

Assuming these are WinForms - you can check if you are currently in DesignMode and just return the function immediately.

There are several difficulties in this article that are fully explained including the solution.

+2
source

You can try this article on MSDN about using the DesignMode > property. It can help you. You can wrap your code that throws an exception in this case in a conditional expression that avoids the code during development.

Note that this does not work in the constructor , because the constructor must create an instance of the object and then set the property.

+1
source

All Articles