系统定义了一个基类控件BaseControl,该类实现控件的通用方法以及进行依赖注入
public partial class BaseControl : UserControl
{
//依赖注入的实体类
public TestClass testClass{ get; set; }
//依赖注入的方法
public void InitInjection()
{
//to do init injection
}
}
子类控件TestControl继承基类控件BaseControl
public class TestControl : BaseControl
{
//to do ...
}
当界面拖拽TestControl到界面或者打开设计视图时,会在界面的designer.cs文件中生成依赖注入的实体类的实例化方法
//TestForm.Designer.cs文件
public class TestForm
{
...
private void InitializeComponent()
{
TestClass testClass = new TestClass();
...
}
}
解决方案:
通过系统System.ComponentModel.Component下的DesignMode来控制打开设计视图或者拖拽控件时,不进行依赖注入
public partial class BaseControl : UserControl
{
pu