首页 新闻 会员 周边

C#封装WinForm控件遇到的问题

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-16 22:44
View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.Windows.Forms;
  7 using System.Drawing;
  8 using System.ComponentModel;
  9 using System.ComponentModel.Design.Serialization;
 10 using System.Reflection;
 11 
 12 namespace CommonControl
 13 {
 14     public class MyListControl : Control
 15     {
 16         [Browsable(true)]
 17         public Scope Scopes { get; set; }
 18     }
 19     [TypeConverter(typeof(ScopeConverter))]
 20     public class Scope
 21     {
 22         private Int32 _min;
 23         private Int32 _max;
 24 
 25         public Scope()
 26         {
 27         }
 28 
 29         public Scope(Int32 min, Int32 max)
 30         {
 31             _min = min;
 32             _max = max;
 33         }
 34 
 35         [Browsable(true)]
 36         public Int32 Min
 37         {
 38             get
 39             {
 40                 return _min;
 41             }
 42             set
 43             {
 44                 _min = value;
 45             }
 46         }
 47 
 48         [Browsable(true)]
 49         public Int32 Max
 50         {
 51             get
 52             {
 53                 return _max;
 54             }
 55             set
 56             {
 57                 _max = value;
 58             }
 59 
 60         }
 61     }
 62     public class ScopeConverter : TypeConverter
 63     {
 64         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 65         {
 66             if (sourceType == typeof(String)) return true;
 67 
 68             return base.CanConvertFrom(context, sourceType);
 69         }
 70 
 71         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 72         {
 73             if (destinationType == typeof(String)) return true;
 74 
 75             if (destinationType == typeof(InstanceDescriptor)) return true;
 76 
 77             return base.CanConvertTo(context, destinationType);
 78         }
 79 
 80         public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
 81         {
 82             String result = "";
 83             if (destinationType == typeof(String))
 84             {
 85                 Scope scope = (Scope)value;
 86                 result = scope.Min.ToString() + "," + scope.Max.ToString();
 87                 return result;
 88 
 89             }
 90 
 91             if (destinationType == typeof(InstanceDescriptor))
 92             {
 93                 ConstructorInfo ci = typeof(Scope).GetConstructor(new Type[] { typeof(Int32), typeof(Int32) });
 94                 Scope scope = (Scope)value;
 95                 return new InstanceDescriptor(ci, new object[] { scope.Min, scope.Max });
 96             }
 97             return base.ConvertTo(context, culture, value, destinationType);
 98         }
 99 
100         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
101         {
102             if (value is string)
103             {
104                 String[] v = ((String)value).Split(',');
105                 if (v.GetLength(0) != 2)
106                 {
107                     throw new ArgumentException("Invalid parameter format");
108                 }
109 
110                 Scope csf = new Scope();
111                 csf.Min = Convert.ToInt32(v[0]);
112                 csf.Max = Convert.ToInt32(v[1]);
113                 return csf;
114             }
115             return base.ConvertFrom(context, culture, value);
116         }
117 
118         public override bool GetPropertiesSupported(ITypeDescriptorContext context)
119         {
120             return true;
121         }
122 
123         public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
124         {
125             return TypeDescriptor.GetProperties(typeof(Scope), attributes);
126         }
127     }
128 }

当在属性面板上填写数字的时候

报错了

小银光的主页 小银光 | 初学一级 | 园豆:25
提问于:2013-03-12 17:04
< >
分享
最佳答案
0

再次添加是不是版本问题,之前遇到一个跟你这类型,修改下Version版本号就可以了.你参考下

收获园豆:10
伏草惟存 | 小虾三级 |园豆:1420 | 2013-03-12 20:04

确实是版本号的原因,改了之后就好了。但是再次将这个控件拖入到界面中又出现了这个问题!

小银光 | 园豆:25 (初学一级) | 2013-03-13 15:33

@小银光: 他这个其实本质就是程序集反射问题,前天学习刚知道

伏草惟存 | 园豆:1420 (小虾三级) | 2013-03-13 15:37

@卿君: 那这个该如何解决呢?

小银光 | 园豆:25 (初学一级) | 2013-03-13 16:11

@小银光: 你运行生产有个dll组件,打开组件后以xml形式显示相关信息找到版本号改一个就可以了。或者你在bin文件夹中将其删除,然后把原始生产的dll拷贝到目标bin文件夹下

伏草惟存 | 园豆:1420 (小虾三级) | 2013-03-13 19:03
其他回答(1)
0

两个dll中有相同的类?类前加命名空间试试,命名空间一样吗?

会长 | 园豆:12401 (专家六级) | 2013-03-12 17:11

不是这问题!

支持(0) 反对(0) 小银光 | 园豆:25 (初学一级) | 2013-03-12 17:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册