C#实例详解TypeConverterAttribute应用

开发 后端
C#实例详解TypeConverterAttribute应用主要向你介绍了在C# WinForm控件开发中的应用实例,希望对你了解和掌握TypeConverterAttribute应用有所帮助

TypeConverterAttribute应用是如何实现的呢?那么这里向你介绍在C# WinForm控件开发中是如何操作的(C#实例详解),希望对你了解TypeConverterAttribute应用有所帮助。

C#实例详解TypeConverterAttribute应用在创建的控件代码中添加一个Scope属性:

  1. [Browsable(true)]  
  2. public Scope Scope  
  3. {  
  4. get 
  5. {  
  6. return _scope;  
  7. }  
  8. set 
  9. {  
  10. _scope = value;  
  11. }  

这个属性的类型是Scope类,代码如下:

  1. public class Scope  
  2. {  
  3. private Int32 _min;  
  4. private Int32 _max;  
  5. public Scope()  
  6. {  
  7. }  
  8. public Scope(Int32 min, Int32 max)  
  9. {  
  10. _min = min;  
  11. _max = max;  
  12. }  
  13. [Browsable(true)]  
  14. public Int32 Min  
  15. {  
  16. get 
  17. {  
  18. return _min;  
  19. }  
  20. set 
  21. {  
  22. _min = value;  
  23. }  
  24. }  
  25.  
  26. [Browsable(true)]  
  27. public Int32 Max  
  28. {  
  29. get 
  30. {  
  31. return _max;  
  32. }  
  33. set 
  34. {  
  35. _max = value;  
  36. }  
  37. }  
  38. }  

添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:

  1. public class ScopeConverter : TypeConverter  
  2. {  
  3. public override bool CanConvertFrom(  
  4. ITypeDescriptorContext context, Type sourceType)  
  5. {  
  6. if (sourceType == typeof(String)) return true;  
  7.  
  8. return base.CanConvertFrom(context, sourceType);  
  9. }  
  10.  
  11. public override bool CanConvertTo(  
  12. ITypeDescriptorContext context, Type destinationType)  
  13. {  
  14. if (destinationType == typeof(String)) return true;  
  15.  
  16. if (destinationType == typeof(InstanceDescriptor)) return true;  
  17.  
  18. return base.CanConvertTo(context, destinationType);  
  19. }  
  20.  
  21. public override object ConvertTo(  
  22. ITypeDescriptorContext context,   
  23. System.Globalization.CultureInfo culture,   
  24. object value, Type destinationType)  
  25. {  
  26. String result = "";  
  27. if (destinationType == typeof(String))  
  28. {  
  29. Scope scope = (Scope)value;  
  30. result = scope.Min.ToString()+"," + scope.Max.ToString();  
  31. return result;  
  32. ///C#实例详解TypeConverterAttribute应用  
  33. }  
  34.  
  35. if (destinationType == typeof(InstanceDescriptor))  
  36. {  
  37. ConstructorInfo ci = typeof(Scope).GetConstructor(  
  38. new Type[] {typeof(Int32),typeof(Int32) });  
  39. Scope scope = (Scope)value;  
  40. return new InstanceDescriptor(ci, new object[] { scope.Min,scope.Max });  
  41. }  
  42. return base.ConvertTo(context, culture, value, destinationType);  
  43. }  
  44.  
  45. public override object ConvertFrom(  
  46. ITypeDescriptorContext context,   
  47. System.Globalization.CultureInfo culture, object value)  
  48. {  
  49. if (value is string)  
  50. {  
  51. String[] v = ((String)value).Split(',');  
  52. if (v.GetLength(0) != 2)  
  53. {  
  54. throw new ArgumentException("Invalid parameter format");  
  55. }  
  56.  
  57. Scope csf = new Scope();  
  58. csf.Min = Convert.ToInt32(v[0]);  
  59. csf.Max = Convert.ToInt32(v[1]);  
  60. return csf;  
  61. }  
  62. return base.ConvertFrom(context, culture, value);  
  63. }  
  64. }  

现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:

  1. [TypeConverter(typeof(ScopeConverter))]  
  2. public class Scope 

添加完以后build工程,然后切换到测试工程,选中控件,在属性浏览器里查看属性,现在的Scope属性可以编辑了,如下图所示:

Scope属性 

我们修改默认的值,然后看看Form设计器为我们生成了什么代码:

  1. this.myListControl1.BackColor =   
  2. System.Drawing.SystemColors.ActiveCaptionText;  
  3. this.myListControl1.Item.Add(1);  
  4. this.myListControl1.Item.Add(2);  
  5. this.myListControl1.Item.Add(3);  
  6. this.myListControl1.Item.Add(6);  
  7. this.myListControl1.Item.Add(8);  
  8. this.myListControl1.Item.Add(9);  
  9. this.myListControl1.Location =   
  10. new System.Drawing.Point(12, 34);  
  11. this.myListControl1.Name = "myListControl1";  
  12. this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);  
  13. this.myListControl1.Size = new System.Drawing.Size(220, 180);  
  14. this.myListControl1.TabIndex = 1;  
  15. this.myListControl1.Text = "myListControl1"

关键是这一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope类的类型转换器为属性提供了实例化的代码。

C#实例详解TypeConverterAttribute应用的相关内容就向你介绍到这里,希望那个对你了解和学习C#实例详解TypeConverterAttribute应用有所帮助。

责任编辑:仲衡 来源: 百度空间
相关推荐

2009-08-28 12:47:30

C#静态方法应用

2009-09-04 18:09:12

C# Main函数

2009-09-02 19:12:37

C#递归

2009-09-01 15:47:20

C#取整函数

2009-09-02 11:18:10

C#动态数组

2009-09-03 18:55:08

C#判断浏览器

2009-09-11 13:03:48

Scope属性

2009-09-02 17:12:06

C#关机代码

2009-08-20 11:01:51

C#操作内存

2009-08-18 10:14:19

C#插件构架

2009-08-17 17:49:20

C# 枚举

2009-08-18 17:05:08

C#操作xml文件

2009-08-26 11:32:37

C#打印文档

2009-09-07 05:50:59

C# Timer用法

2009-08-28 13:12:56

C#反射实例C#反射

2009-08-26 11:07:36

C#打印窗体

2009-08-21 10:13:02

C#异步初步

2009-09-01 11:25:08

C#读取Word文件

2009-08-26 09:22:44

C#实现打印功能

2009-09-03 15:43:21

C#时间计算
点赞
收藏

51CTO技术栈公众号