溫馨提示×

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

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

C#中Scope屬性如何使用

發(fā)布時(shí)間:2021-07-08 15:35:31 來源:億速云 閱讀:471 作者:Leah 欄目:編程語(yǔ)言

本篇文章為大家展示了C#中Scope屬性如何使用,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

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

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

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

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

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;  ///Scope屬性在C#中的應(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);  }   public override bool GetPropertiesSupported(  ITypeDescriptorContext context)  {  return true;  }  ///Scope屬性在C#中的應(yīng)用  public override PropertyDescriptorCollection   GetProperties(ITypeDescriptorContext context,   object value, Attribute[] attributes)  {  return TypeDescriptor.GetProperties(  typeof(Scope), attributes);  }  }

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

C#中Scope屬性如何使用

上述內(nèi)容就是C#中Scope屬性如何使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(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