visual studio 设计器 不能实例化 抽象类的几种解决方案
2011-11-28 13:03
696 查看
The designer must create an instance of type '<type name>', but it can't because the type is declared as abstract
问题产生的原因:This error occurred because the base class of the object being passed to the designer is abstract, which is not allowed.
三种解决方案:
1. 使用 TypeDescriptionProvider 属性
[TypeDescriptionProvider(typeof(SubstituteBaseUserControlProvider))]
public abstract class BaseUserControl: UserControl
在抽象版本的类上加注特性
这样基本可以解决vs设计器不能加载界面的毛病,注意我自己测试的结果比较有古怪,vs2010 net3.5 环境编译后并不能立刻就显示UI,我得把vs关闭后重新打开才能看到效果。
另外:我也测试了一下vs2012并不需要做上面的处理,他本身就支持抽象UI类的继承
2. 不实用 abstract 关键字
实现这些抽象方法 ( abstract method )并在方法中抛出异常
3. 使用一个中间类
假设有如下代码:
public class Form1 : BaseForm ... public abstract class BaseForm : Form
将代码改成这样的形式:
public class Form1 : MiddleClass ... public class MiddleClass : BaseForm ... public abstract class BaseForm : Form ...
问题就会得到解决。
更进一步你可以在 Form1.cs用:
publicclass Form1
#if DEBUG
: MiddleClass
#else
: BaseForm
#endif
这样实际release的代码的行为与原代码并无区别。
Tag: 在设计器中无法创建抽象的实例 2011-11-28 1:08 PM IS2120@CSDN
相关文章推荐
- Visual Studio 2013 MFC基于对话框的程序中加入TeeChart控件后,编译时显示不能实例化抽象类的问题
- const、readonly、抽象类、接口修饰符、抽象类不能被实例化、页面生命周期
- 错误:error C2259:“CxxDlg”:不能实例化抽象类
- 为什么抽象类不能实例化的解释
- 抽象类不能实例化
- 设计一个类,该类不能被继承,且只能实例化3次
- Visual Studio 2008 WPF设计器智能提示(Intellisense)、大纲显示功能无效的解决方案(转)
- VS2010 MFC中“CException”: 不能实例化抽象类
- 类不能实例化对象?(定义的也不是抽象类)
- Visual Studio 2005/2008 SCSF 项目解决方案资源管理器里不能看到Smart Client Factory菜单
- Microsoft Visual Studio 2005 设计窗口不能打开解决方案
- 关于Visual Studio和SQL Server共同安装时,后者安装路径不能修改的解决方案。
- 由于设计页面需要,要把两个并排显示的 div 实现一样高的效果, n 行 n 列布局,每列高度(事先并不能确定哪列的高度)相同,每个设计师追求的目标。方法有以下几种: 1 JS 实现(判断 2 个 d
- PHP中的特殊类,接口类和抽象类(都不能直接实例化)
- Visual Studio中Silverlight设计器加载失败以及解决方案
- PowerDesigner 设计概念模型时属性不能重名的解决方案
- c++ 设计一个不能被继承的类,但可以实例化
- 抽象类不只是不能实例化
- 不能实例化抽象类的浅尝
- Visual Studio 2013编辑HTML文件无设计视图的解决方案