实例详解Scope属性在C#中的应用

Scope属性在C#中的应用有很多,这里向你介绍Scope属性实现C#控件开发中复杂属性的子属性的编辑功能,希望通过实例使你加深对Scope属性的认识。

十年来,创新互联不忘初心,以网站建设互联网行业服务标杆为目标,不断提升技术设计服务水平,帮助客户在互联网推广自己的产品、服务和品牌,为客户创造价值从而实现自身价值!

Scope属性在C#中的应用的思路:

我们给控件添加一个复杂的类型Scope,并且给它的类型提供的一个类型转换器,现在我们可以在属性浏览器中编辑它的值,并且它的值也被串行化的源代码里了。但是你有没有发现,在属性浏览器里编辑这个属性的值还是不太方便。因为属性只是“10,200”这种形式的,所以,你必须按照这种格式来修改,一旦格式错误就会引发异常,比如输入一个“10200”。我们期望这个属性的每一子属性都能够被独立的编辑就好了,这并非不能实现,而且实现还很简单。

为了在属性浏览器里能够独立的编辑子属性,我们还要重写两个方法: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属性在C#中的应用的相关内容就向你介绍到这里,希望那个对你了解和学习Scope属性有所帮助。

【编辑推荐】

  1. 浅析Attribute在C# WinForm控件开发中的使用
  2. 浅谈C#控件属性串行化的实现
  3. C#实例详解TypeConverterAttribute应用
  4. C#类型转换器的实现浅析
  5. 探讨Scope属性在C#和VC++中的使用

网站名称:实例详解Scope属性在C#中的应用
网址分享:http://www.hantingmc.com/qtweb/news33/242233.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联