C#中TypeConverterAttribute如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联公司是专业的义县网站建设公司,义县接单;提供网站建设、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行义县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
C#实例详解TypeConverterAttribute应用在创建的控件代码中添加一个Scope属性:
[Browsable(true)]  public Scope Scope  {  get {  return _scope;  }  set {  _scope = value;  }  }这个属性的类型是Scope类,代码如下:
public class Scope  {  private Int32 _min;  private Int32 _max;  public Scope()  {  }  public Scope(Int32 min, Int32 max)  {  _min = min;  _max = max;  }  [Browsable(true)]  public Int32 Min  {  get {  return _min;  }  set {  _min = value;  }  }   [Browsable(true)]  public Int32 Max  {  get {  return _max;  }  set {  _max = value;  }  }  }添加完属性后,build控件工程,然后在测试的工程里选中添加的控件,然后在属性浏览器里观察它的属性,发现Scope属性是灰的,不能编辑。前一篇文章提到了,在属性浏览器里可以编辑的属性都是有类型转换器的,而.NET框架为基本的类型和常用的类型都提供了默认的类型转换器。接下来我们为Scope类添加一个类型转换器,以便这个属性能够被编辑,而且也可以在源代码文件里自动生成相应的代码。下面是类型转换器的代码:
public class ScopeConverter : TypeConverter  {  public override bool CanConvertFrom(  ITypeDescriptorContext context, Type sourceType)  {  if (sourceType == typeof(String)) return true;   return base.CanConvertFrom(context, sourceType);  }   public override bool CanConvertTo(  ITypeDescriptorContext context, Type destinationType)  {  if (destinationType == typeof(String)) return true;   if (destinationType == typeof(InstanceDescriptor)) return true;   return base.CanConvertTo(context, destinationType);  }   public override object ConvertTo(  ITypeDescriptorContext context,   System.Globalization.CultureInfo culture,   object value, Type destinationType)  {  String result = "";  if (destinationType == typeof(String))  {  Scope scope = (Scope)value;  result = scope.Min.ToString()+"," + scope.Max.ToString();  return result;  ///C#实例详解TypeConverterAttribute应用  }   if (destinationType == typeof(InstanceDescriptor))  {  ConstructorInfo ci = typeof(Scope).GetConstructor(  new Type[] {typeof(Int32),typeof(Int32) });  Scope scope = (Scope)value;  return new InstanceDescriptor(ci, new object[] { scope.Min,scope.Max });  }  return base.ConvertTo(context, culture, value, destinationType);  }   public override object ConvertFrom(  ITypeDescriptorContext context,   System.Globalization.CultureInfo culture, object value)  {  if (value is string)  {  String[] v = ((String)value).Split(',');  if (v.GetLength(0) != 2)  {  throw new ArgumentException("Invalid parameter format");  }   Scope csf = new Scope();  csf.Min = Convert.ToInt32(v[0]);  csf.Max = Convert.ToInt32(v[1]);  return csf;  }  return base.ConvertFrom(context, culture, value);  }  }现在我们为类型提供类型转换器,我们在类型前面添加一个TypeConverterAttribute,如下:
[TypeConverter(typeof(ScopeConverter))] public class Scope
添加完以后build工程,然后切换到测试工程,选中控件,在属性浏览器里查看属性,现在的Scope属性可以编辑了,如下图所示:

我们修改默认的值,然后看看Form设计器为我们生成了什么代码:
this.myListControl1.BackColor = System.Drawing.SystemColors.ActiveCaptionText; this.myListControl1.Item.Add(1); this.myListControl1.Item.Add(2); this.myListControl1.Item.Add(3); this.myListControl1.Item.Add(6); this.myListControl1.Item.Add(8); this.myListControl1.Item.Add(9); this.myListControl1.Location = new System.Drawing.Point(12, 34); this.myListControl1.Name = "myListControl1"; this.myListControl1.Scope = new CustomControlSample.Scope(10, 200); this.myListControl1.Size = new System.Drawing.Size(220, 180); this.myListControl1.TabIndex = 1; this.myListControl1.Text = "myListControl1";
关键是这一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope类的类型转换器为属性提供了实例化的代码。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
本文名称:C#中TypeConverterAttribute如何使用
URL网址:http://www.cqwzjz.cn/article/ihjjpi.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 