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

實例詳解Scope屬性在C#中的應用

開發(fā) 后端
Scope屬性在C#中的應用是如何實現(xiàn)的呢?涉及到的類又是什么呢?那么這里就向你介紹C#控件開發(fā)中的具體的應用操作,以及具體的代碼實現(xiàn),希望對你有所幫助。

Scope屬性在C#中的應用有很多,這里向你介紹Scope屬性實現(xiàn)C#控件開發(fā)中復雜屬性的子屬性的編輯功能,希望通過實例使你加深對Scope屬性的認識。

Scope屬性在C#中的應用的思路:

我們給控件添加一個復雜的類型Scope,并且給它的類型提供的一個類型轉換器,現(xiàn)在我們可以在屬性瀏覽器中編輯它的值,并且它的值也被串行化的源代碼里了。但是你有沒有發(fā)現(xiàn),在屬性瀏覽器里編輯這個屬性的值還是不太方便。因為屬性只是“10,200”這種形式的,所以,你必須按照這種格式來修改,一旦格式錯誤就會引發(fā)異常,比如輸入一個“10200”。我們期望這個屬性的每一子屬性都能夠被獨立的編輯就好了,這并非不能實現(xiàn),而且實現(xiàn)還很簡單。

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

Scope屬性在C#中的應用實例代碼:

  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#中的應用  
  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#中的應用  
  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類的所有的屬性描述器并返回。如果你對TypeDescriptor還不熟悉的話,可以參考MSDN。重寫這兩個方法并編譯以后,在測試工程里查看控件的屬性,你可以看到Scope是如下的形式:

Scope屬性 

Scope屬性在C#中的應用的相關內容就向你介紹到這里,希望那個對你了解和學習Scope屬性有所幫助。

【編輯推薦】

  1. 淺析Attribute在C# WinForm控件開發(fā)中的使用
  2. 淺談C#控件屬性串行化的實現(xiàn)
  3. C#實例詳解TypeConverterAttribute應用
  4. C#類型轉換器的實現(xiàn)淺析
  5. 探討Scope屬性在C#和VC++中的使用
責任編輯:仲衡 來源: CSDN博客
相關推薦

2009-09-11 12:50:34

Scope屬性

2009-09-02 19:12:37

C#遞歸

2009-09-11 12:31:52

C#實例詳解TypeConvert

2009-09-02 11:18:10

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

2009-08-28 12:47:30

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

2009-09-04 18:09:12

C# Main函數(shù)

2009-09-01 15:47:20

C#取整函數(shù)

2009-09-03 18:55:08

C#判斷瀏覽器

2009-08-21 10:43:19

FlyTcpFrame

2009-08-24 16:39:19

C# 泛型應用

2009-09-02 17:12:06

C#關機代碼

2009-08-20 11:01:51

C#操作內存

2009-08-18 10:14:19

C#插件構架

2009-09-02 18:44:19

C#遞歸

2009-08-17 17:49:20

C# 枚舉

2024-10-21 07:05:14

C#特性語言

2024-07-10 08:31:59

C#特性代碼

2009-01-19 10:26:02

C#Namespace.NET

2009-08-18 17:05:08

C#操作xml文件

2009-08-28 13:12:56

C#反射實例C#反射
點贊
收藏

51CTO技術棧公眾號