VisualStudio 2010 Designer throws an implemented virtual method

I want to have an abstract UserControl , BaseControl , which implements the IBaseControl interface. However, setting the class for abstract breaks is VisualStudio Designer (this is a known issue with Visual Studio (for example, see https://stackoverflow.com/questions/890189/ ... for more information), and as far as I know, expected changes are not expected there soon

So, to get around this, I am making BaseControl not abstract, but its implementation of the IBaseControl methods virtual. However, since these methods do not make sense for BaseControl (for example, not all components have been added yet), I force them to throw:

 public class BaseControl : UserControl, IBaseControl { /// <summary> /// This IBaseControl method is not abstract because /// that breaks the Designer /// </summary> public virtual void LoadSettings() { throw new NotImplementedException("Implement in derived class."); } private void BaseControl_Load(object sender, EventArgs e) { // intention: derived methods automagically load their settings this.LoadSettings(); } } 

In the derived control, I have a corresponding override:

 public partial class DerivedControl : BaseControl { public override void LoadSettings() { // load settings } } 

Despite this, when I try to open the control in the constructor, I get an error message indicating that BaseControl.LoadSettings exception.

Now remember that LoadSettings is called in the base class, so when the constructor loads DerivedControl , it in turn calls the load method for BaseControl , which throws.

Are you having a similar problem? How did you deal with this? I would like to have an elegant solution, if possible.

+6
source share
1 answer

The reason the exception is thrown is because, oddly enough, the developer does not compile or create an instance of the class that you are designing at all ! It only compiles and instantiates the base class of the control you are developing.

Why this is so, it becomes apparent when you realize that adding new subelements to an element will require further recompilation. In addition, each event handler that you add changes the class and again requires recompilation. Since event handlers will never be called in the designer, all this is completely unnecessary. You create a class that resembles your class, but not your class; this is a work in progress.

Since only the base class is created, the base class cannot be abstract and must be functional , just like . If you throw exceptions, the designer will see them. The only practical solution is either:

  • Do not exclude exceptions from the base class or
  • Conditionally discard exceptions depending on whether it is development time or not.

Or everything will work; Use whatever you prefer or best suits your design. This is exactly how the designer works, and, as you already mentioned, this is unlikely to change.

+6
source

Source: https://habr.com/ru/post/890187/


All Articles