自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實(shí)例詳解TypeConverterAttribute應(yīng)用

開發(fā) 后端
C#實(shí)例詳解TypeConverterAttribute應(yīng)用主要向你介紹了在C# WinForm控件開發(fā)中的應(yīng)用實(shí)例,希望對你了解和掌握TypeConverterAttribute應(yīng)用有所幫助

TypeConverterAttribute應(yīng)用是如何實(shí)現(xiàn)的呢?那么這里向你介紹在C# WinForm控件開發(fā)中是如何操作的(C#實(shí)例詳解),希望對你了解TypeConverterAttribute應(yīng)用有所幫助。

C#實(shí)例詳解TypeConverterAttribute應(yīng)用在創(chuàng)建的控件代碼中添加一個(gè)Scope屬性:

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

這個(gè)屬性的類型是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控件工程,然后在測試的工程里選中添加的控件,然后在屬性瀏覽器里觀察它的屬性,發(fā)現(xiàn)Scope屬性是灰的,不能編輯。前一篇文章提到了,在屬性瀏覽器里可以編輯的屬性都是有類型轉(zhuǎn)換器的,而.NET框架為基本的類型和常用的類型都提供了默認(rèn)的類型轉(zhuǎn)換器。接下來我們?yōu)镾cope類添加一個(gè)類型轉(zhuǎn)換器,以便這個(gè)屬性能夠被編輯,而且也可以在源代碼文件里自動(dòng)生成相應(yīng)的代碼。下面是類型轉(zhuǎn)換器的代碼:

  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#實(shí)例詳解TypeConverterAttribute應(yīng)用  
  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. }  

現(xiàn)在我們?yōu)轭愋吞峁╊愋娃D(zhuǎn)換器,我們在類型前面添加一個(gè)TypeConverterAttribute,如下:

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

添加完以后build工程,然后切換到測試工程,選中控件,在屬性瀏覽器里查看屬性,現(xiàn)在的Scope屬性可以編輯了,如下圖所示:

Scope屬性 

我們修改默認(rèn)的值,然后看看Form設(shè)計(jì)器為我們生成了什么代碼:

  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"

關(guān)鍵是這一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope類的類型轉(zhuǎn)換器為屬性提供了實(shí)例化的代碼。

C#實(shí)例詳解TypeConverterAttribute應(yīng)用的相關(guān)內(nèi)容就向你介紹到這里,希望那個(gè)對你了解和學(xué)習(xí)C#實(shí)例詳解TypeConverterAttribute應(yīng)用有所幫助。

責(zé)任編輯:仲衡 來源: 百度空間
相關(guān)推薦

2009-08-28 12:47:30

C#靜態(tài)方法應(yīng)用

2009-09-04 18:09:12

C# Main函數(shù)

2009-09-02 19:12:37

C#遞歸

2009-09-01 15:47:20

C#取整函數(shù)

2009-09-02 11:18:10

C#動(dòng)態(tài)數(shù)組

2009-09-03 18:55:08

C#判斷瀏覽器

2009-09-02 17:12:06

C#關(guān)機(jī)代碼

2009-08-20 11:01:51

C#操作內(nèi)存

2009-08-18 10:14:19

C#插件構(gòu)架

2009-09-11 13:03:48

Scope屬性

2009-08-17 17:49:20

C# 枚舉

2024-10-21 07:05:14

C#特性語言

2024-07-10 08:31:59

C#特性代碼

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 13:12:56

C#反射實(shí)例C#反射

2009-08-21 10:13:02

C#異步初步

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-09-07 05:50:59

C# Timer用法

2009-08-26 11:32:37

C#打印文檔
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號