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

十年來,創(chuàng)新互聯(lián)不忘初心,以網(wǎng)站建設(shè)互聯(lián)網(wǎng)行業(yè)服務(wù)標(biāo)桿為目標(biāo),不斷提升技術(shù)設(shè)計服務(wù)水平,幫助客戶在互聯(lián)網(wǎng)推廣自己的產(chǎn)品、服務(wù)和品牌,為客戶創(chuàng)造價值從而實現(xiàn)自身價值!
Scope屬性在C#中的應(yīng)用的思路:
我們給控件添加一個復(fù)雜的類型Scope,并且給它的類型提供的一個類型轉(zhuǎn)換器,現(xiàn)在我們可以在屬性瀏覽器中編輯它的值,并且它的值也被串行化的源代碼里了。但是你有沒有發(fā)現(xiàn),在屬性瀏覽器里編輯這個屬性的值還是不太方便。因為屬性只是“10,200”這種形式的,所以,你必須按照這種格式來修改,一旦格式錯誤就會引發(fā)異常,比如輸入一個“10200”。我們期望這個屬性的每一子屬性都能夠被獨立的編輯就好了,這并非不能實現(xiàn),而且實現(xiàn)還很簡單。
為了在屬性瀏覽器里能夠獨立的編輯子屬性,我們還要重寫兩個方法:GetPropertiesSupported()和GetProperties();下面是ScopeConverter的完整代碼:
Scope屬性在C#中的應(yīng)用實例代碼:
- 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類的所有的屬性描述器并返回。如果你對TypeDescriptor還不熟悉的話,可以參考MSDN。重寫這兩個方法并編譯以后,在測試工程里查看控件的屬性,你可以看到Scope是如下的形式:
Scope屬性在C#中的應(yīng)用的相關(guān)內(nèi)容就向你介紹到這里,希望那個對你了解和學(xué)習(xí)Scope屬性有所幫助。
【編輯推薦】
- 淺析Attribute在C# WinForm控件開發(fā)中的使用
- 淺談C#控件屬性串行化的實現(xiàn)
- C#實例詳解TypeConverterAttribute應(yīng)用
- C#類型轉(zhuǎn)換器的實現(xiàn)淺析
- 探討Scope屬性在C#和VC++中的使用
分享標(biāo)題:實例詳解Scope屬性在C#中的應(yīng)用
URL分享:http://m.5511xx.com/article/dppeisp.html


咨詢
建站咨詢
