自定义控件 属性扩展 多次“重新生成” 后 发生错误。

wlllll 2008-03-17 05:58:31
自定义控件 属性扩展 多次“重新生成” 后 发生错误。

环境:V.C#2005

下载附件。打开工程后,选择在Form1。在工具箱中选取TestComboBox控件。


放置Form1任何位置。选择放置后的testComboBox1控件。打开属性面板。查看属性最顶端。
[img=http://dl2.csdn.net/down4/20080317/17174557995.jpg[/img]

编辑属性组后 宽高都为1后。在Form1.Designer.cs原代码中会增加:
this.testComboBox1.TestSize = new ClassTestSize(1, 1);



“重新生成解决方案”多次后,会导致testComboBox1控件的自定义属性发生错误。

第一次生成:
根属性字符不显示了。
[img=http://dl2.csdn.net/down4/20080317/17175216841.jpg[/img]

第二次生成:
属性值都变成0了。
[img=http://dl2.csdn.net/down4/20080317/17174741321.jpg[/img]

而且在设置任何一个值的时候,会出现这样的错误:
类型“ClassTestSize”的对象无法转换为类型“ClassTestSize”。
[img=http://dl2.csdn.net/down4/20080317/17175447372.jpg[/img]


附件:http://dl2.csdn.net/down4/20080317/17175753503.rar
...全文
114 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lalac 2008-03-17
  • 打赏
  • 举报
回复
lz不用在这里磨了,这个是IDE的cache造成的问题,由于IDE的设计器需要使用自定义控件,就加载了assembly,这个只有在appdomain卸载时才能卸载,这就导致IDE在多次编译之后,出现“类型“ClassTestSize”的对象无法转换为类型"ClassTestSize"。”——所以这个问题是正常的。

建议lz给VS2005打上sp1,会稍微好那么一点点。

如果不想让vs05多占据1-2G空间,还有一个办法是:遇到这种情况之后把IDE关掉重新打开就好了。
wlllll 2008-03-17
  • 打赏
  • 举报
回复
wlllll 2008-03-17
  • 打赏
  • 举报
回复
wlllll 2008-03-17
  • 打赏
  • 举报
回复
wlllll 2008-03-17
  • 打赏
  • 举报
回复
控件代码:

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Collections;

[TypeConverter(typeof(ClassTestSizeTypeConverter))]
public class ClassTestSize {
private Int32 AttrWidth;
private Int32 AttrHeight;
public ClassTestSize() {
}
public ClassTestSize(Int32 DataWidth, Int32 DataHeight) {
Width = DataWidth;
Height = DataHeight;
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Width {
get { return AttrWidth; }
set { AttrWidth = value; }
}
[DefaultValueAttribute(0), Description("实验宽")]
public Int32 Height {
get { return AttrHeight; }
set { AttrHeight = value; }
}
}
public class ClassTestSizeTypeConverter : TypeConverter {
public override Boolean CanConvertFrom(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
return base.CanConvertFrom(DataText, DataType);
}
public override Object ConvertFrom(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue) {
if (DataValue is String) {
String[] HanText = ((String)DataValue).Split(',');
if (HanText.GetLength(0) != 2) {
throw new ArgumentException("长度不正确");
}
ClassTestSize DataObj = new ClassTestSize();
DataObj.Width = int.Parse(HanText[0]);
DataObj.Height = int.Parse(HanText[1]);
return DataObj;
}
return base.ConvertFrom(DataText, DataCI, DataValue);
}

public override Boolean CanConvertTo(ITypeDescriptorContext DataText, Type DataType) {
if (DataType == typeof(String)) {
return true;
}
if (DataType == typeof(InstanceDescriptor)) {
return true;
}
return base.CanConvertTo(DataText, DataType);
}
public override Object ConvertTo(ITypeDescriptorContext DataText, CultureInfo DataCI, Object DataValue, Type DataType) {
if (DataType == typeof(String)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
return (Object)(HanObj.Width.ToString() + "," + HanObj.Height.ToString());
}
if (DataType == typeof(InstanceDescriptor)) {
ClassTestSize HanObj = (ClassTestSize)DataValue;
ConstructorInfo HanCI = typeof(ClassTestSize).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
return (Object)(new InstanceDescriptor(HanCI, new object[] { HanObj.Width, HanObj.Height }));
}
return base.ConvertTo(DataText, DataCI, DataValue, DataType);
}

public override Boolean GetPropertiesSupported(ITypeDescriptorContext DataText) {
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext DataText, Object DataValue, Attribute[] Attributes) {
PropertyDescriptorCollection HanProperties = TypeDescriptor.GetProperties(DataValue);
return HanProperties;
}

public override Boolean GetCreateInstanceSupported(ITypeDescriptorContext DataText) {
return true;
}
public override Object CreateInstance(ITypeDescriptorContext DataText, IDictionary PropertyValues) {
return new ClassTestSize((Int32)PropertyValues["Width"], (Int32)PropertyValues["Height"]);
}
}
public class TestComboBox : ComboBox {
[CategoryAttribute(" 自定义属性组1")]
[DisplayName(" TestSize")]
[TypeConverter(typeof(ClassTestSize))]
public ClassTestSize TestSize {
get { return AttrTestSize; }
set { AttrTestSize = value; }
}
public ClassTestSize AttrTestSize = new ClassTestSize();
}

wlllll 2008-03-17
  • 打赏
  • 举报
回复
自定义控件 属性扩展 多次“重新生成” 后 发生错误。

环境:V.C#2005

下载附件。打开工程后,选择在Form1。在工具箱中选取TestComboBox控件。


放置Form1任何位置。选择放置后的testComboBox1控件。打开属性面板。查看属性最顶端。


编辑属性组后 宽高都为1后。在Form1.Designer.cs原代码中会增加:
this.testComboBox1.TestSize = new ClassTestSize(1, 1);



“重新生成解决方案”多次后,会导致testComboBox1控件的自定义属性发生错误。

第一次生成:
根属性字符不显示了。


第二次生成:
属性值都变成0了。


而且在设置任何一个值的时候,会出现这样的错误:
类型“ClassTestSize”的对象无法转换为类型“ClassTestSize”。



附件:http://dl2.csdn.net/down4/20080317/17175753503.rar

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧