溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中TypeConverterAttribute如何使用

發(fā)布時(shí)間:2021-07-07 17:50:37 來(lái)源:億速云 閱讀:450 作者:Leah 欄目:編程語(yǔ)言

C#中TypeConverterAttribute如何使用,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

C#實(shí)例詳解TypeConverterAttribute應(yīng)用在創(chuàng)建的控件代碼中添加一個(gè)Scope屬性:

[Browsable(true)]  public Scope Scope  {  get {  return _scope;  }  set {  _scope = value;  }  }

這個(gè)屬性的類(lèi)型是Scope類(lèi),代碼如下:

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

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#實(shí)例詳解TypeConverterAttribute應(yīng)用  }   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);  }  }

現(xiàn)在我們?yōu)轭?lèi)型提供類(lèi)型轉(zhuǎn)換器,我們?cè)陬?lèi)型前面添加一個(gè)TypeConverterAttribute,如下:

[TypeConverter(typeof(ScopeConverter))]  public class Scope

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

C#中TypeConverterAttribute如何使用

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

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";

關(guān)鍵是這一行this.myListControl1.Scope = new CustomControlSample.Scope(10, 200),Scope類(lèi)的類(lèi)型轉(zhuǎn)換器為屬性提供了實(shí)例化的代碼。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI