成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

TypeConverterAttribute應(yīng)用是如何實(shí)現(xiàn)的呢?那么這里向你介紹在C# WinForm控件開(kāi)發(fā)中是如何操作的(C#實(shí)例詳解),希望對(duì)你了解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è)屬性的類(lèi)型是Scope類(lèi),代碼如下:

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

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

添加完以后build工程,然后切換到測(cè)試工程,選中控件,在屬性瀏覽器里查看屬性,現(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類(lèi)的類(lèi)型轉(zhuǎn)換器為屬性提供了實(shí)例化的代碼。

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

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

2009-09-02 19:12:37

C#遞歸

2009-09-04 18:09:12

C# Main函數(shù)

2009-08-28 12:47:30

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

2009-09-02 11:18:10

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

2009-09-01 15:47:20

C#取整函數(shù)

2009-09-03 18:55:08

C#判斷瀏覽器

2009-08-18 10:14:19

C#插件構(gòu)架

2009-08-20 11:01:51

C#操作內(nèi)存

2009-09-02 17:12:06

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

2009-09-11 13:03:48

Scope屬性

2009-08-17 17:49:20

C# 枚舉

2024-07-10 08:31:59

C#特性代碼

2024-10-21 07:05:14

C#特性語(yǔ)言

2009-08-28 13:12:56

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

2009-09-07 05:50:59

C# Timer用法

2009-08-21 10:13:02

C#異步初步

2009-09-01 11:25:08

C#讀取Word文件

2009-08-26 09:22:44

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

2009-08-26 11:07:36

C#打印窗體

2009-08-26 11:32:37

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

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

主站蜘蛛池模板: 欧美mv日韩mv国产网站91进入 | 亚洲a视频 | 精品99在线 | 日韩一区二区福利 | 免费观看日韩精品 | 国产亚洲精品精品国产亚洲综合 | 久久毛片网站 | 欧美一区二区三区日韩 | 亚洲最新在线 | 亚欧精品 | 亚洲精品一区二区另类图片 | 免费成人高清在线视频 | 国产免费一区二区 | 久久久久久久久久久久久久国产 | 特一级毛片 | 一级毛片视频免费观看 | 国产精品久久久久久吹潮日韩动画 | 欧美成人精品 | 色视频网站在线观看 | 精品国产区 | 午夜视频在线 | 免费在线性爱视频 | 久草免费电影 | 狠狠操电影 | 国产精品毛片 | 国产精品资源在线观看 | 日韩av一区二区在线观看 | 伊人网在线播放 | 亚洲国产成人在线 | 国产精品入口久久 | 亚洲精品一区二区三区在线 | 亚洲电影成人 | 欧美成人影院 | 91se在线 | 久久久久久亚洲精品 | 欧美一区二区三区视频 | 黄色av观看 | 国产成人精品一区二区三区在线 | 国产午夜精品一区二区三区嫩草 | 日本福利视频免费观看 | 日日干日日操 |