日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#實例詳解TypeConverterAttribute應(yīng)用

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

南關(guān)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

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

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

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

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

 
 
 
  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)換器為屬性提供了實例化的代碼。

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


文章題目:C#實例詳解TypeConverterAttribute應(yīng)用
路徑分享:http://www.5511xx.com/article/dhcseji.html