日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
實(shí)例詳解Scope屬性在C#中的應(yīng)用

Scope屬性在C#中的應(yīng)用有很多,這里向你介紹Scope屬性實(shí)現(xiàn)C#控件開(kāi)發(fā)中復(fù)雜屬性的子屬性的編輯功能,希望通過(guò)實(shí)例使你加深對(duì)Scope屬性的認(rèn)識(shí)。

十年來(lái),創(chuàng)新互聯(lián)不忘初心,以網(wǎng)站建設(shè)互聯(lián)網(wǎng)行業(yè)服務(wù)標(biāo)桿為目標(biāo),不斷提升技術(shù)設(shè)計(jì)服務(wù)水平,幫助客戶在互聯(lián)網(wǎng)推廣自己的產(chǎn)品、服務(wù)和品牌,為客戶創(chuàng)造價(jià)值從而實(shí)現(xiàn)自身價(jià)值!

Scope屬性在C#中的應(yīng)用的思路:

我們給控件添加一個(gè)復(fù)雜的類(lèi)型Scope,并且給它的類(lèi)型提供的一個(gè)類(lèi)型轉(zhuǎn)換器,現(xiàn)在我們可以在屬性瀏覽器中編輯它的值,并且它的值也被串行化的源代碼里了。但是你有沒(méi)有發(fā)現(xiàn),在屬性瀏覽器里編輯這個(gè)屬性的值還是不太方便。因?yàn)閷傩灾皇恰?0,200”這種形式的,所以,你必須按照這種格式來(lái)修改,一旦格式錯(cuò)誤就會(huì)引發(fā)異常,比如輸入一個(gè)“10200”。我們期望這個(gè)屬性的每一子屬性都能夠被獨(dú)立的編輯就好了,這并非不能實(shí)現(xiàn),而且實(shí)現(xiàn)還很簡(jiǎn)單。

為了在屬性瀏覽器里能夠獨(dú)立的編輯子屬性,我們還要重寫(xiě)兩個(gè)方法:GetPropertiesSupported()和GetProperties();下面是ScopeConverter的完整代碼:

Scope屬性在C#中的應(yīng)用實(shí)例代碼:

 
 
 
 
  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 ==   
  17. typeof(InstanceDescriptor)) return true;  
  18.  
  19. return base.CanConvertTo(context, destinationType);  
  20. }  
  21.  
  22. public override object ConvertTo(  
  23. ITypeDescriptorContext context,   
  24. System.Globalization.CultureInfo culture,  
  25.  object value, Type destinationType)  
  26. {  
  27. String result = "";  
  28. if (destinationType == typeof(String))  
  29. {  
  30. Scope scope = (Scope)value;  
  31. result = scope.Min.ToString()+"," + scope.Max.ToString();  
  32. return result;  
  33. ///Scope屬性在C#中的應(yīng)用  
  34. }  
  35.  
  36. if (destinationType == typeof(InstanceDescriptor))  
  37. {  
  38. ConstructorInfo ci = typeof(Scope).GetConstructor(  
  39. new Type[] {typeof(Int32),typeof(Int32) });  
  40. Scope scope = (Scope)value;  
  41. return new InstanceDescriptor(ci,   
  42. new object[] { scope.Min,scope.Max });  
  43. }  
  44. return base.ConvertTo(context,   
  45. culture, value, destinationType);  
  46. }  
  47.  
  48. public override object ConvertFrom(  
  49. ITypeDescriptorContext context,   
  50. System.Globalization.CultureInfo culture, object value)  
  51. {  
  52. if (value is string)  
  53. {  
  54. String[] v = ((String)value).Split(',');  
  55. if (v.GetLength(0) != 2)  
  56. {  
  57. throw new ArgumentException("Invalid parameter format");  
  58. }  
  59.  
  60. Scope csf = new Scope();  
  61. csf.Min = Convert.ToInt32(v[0]);  
  62. csf.Max = Convert.ToInt32(v[1]);  
  63. return csf;  
  64. }  
  65. return base.ConvertFrom(context, culture, value);  
  66. }  
  67.  
  68. public override bool GetPropertiesSupported(  
  69. ITypeDescriptorContext context)  
  70. {  
  71. return true;  
  72. }  
  73. ///Scope屬性在C#中的應(yīng)用  
  74. public override PropertyDescriptorCollection   
  75. GetProperties(ITypeDescriptorContext context,   
  76. object value, Attribute[] attributes)  
  77. {  
  78. return TypeDescriptor.GetProperties(  
  79. typeof(Scope), attributes);  
  80. }  
  81. }  

在GetProperties方法里,我用TypeDescriptor獲得了Scope類(lèi)的所有的屬性描述器并返回。如果你對(duì)TypeDescriptor還不熟悉的話,可以參考MSDN。重寫(xiě)這兩個(gè)方法并編譯以后,在測(cè)試工程里查看控件的屬性,你可以看到Scope是如下的形式:

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

【編輯推薦】

  1. 淺析Attribute在C# WinForm控件開(kāi)發(fā)中的使用
  2. 淺談C#控件屬性串行化的實(shí)現(xiàn)
  3. C#實(shí)例詳解TypeConverterAttribute應(yīng)用
  4. C#類(lèi)型轉(zhuǎn)換器的實(shí)現(xiàn)淺析
  5. 探討Scope屬性在C#和VC++中的使用

本文名稱:實(shí)例詳解Scope屬性在C#中的應(yīng)用
URL網(wǎng)址:http://www.dlmjj.cn/article/dppeisp.html