C#類型轉換器的實現淺析
C#類型轉換器的實現是如何的呢?具體的操作是什么呢?那么這里就向你介紹C#類型轉換器的實現需要重寫四個方法,下面我們來具體看看細節是如何操作的。
C#類型轉換器的實現所用重寫的方法:
◆CanConvertFrom()――根據類型參數進行測試,判斷是否能從這個類型轉換成當前類型,在本例中我們只提供轉換string和InstanceDescriptor類型的能力。
◆CanConvertTo()――根據類型參數進行測試,判斷是否能從當前類型轉換成指定的類型。
◆ConvertTo()――將參數value的值轉換為指定的類型。
◆ConvertFrom()――串換參數value,并返回但書類型的一個對象。
C#類型轉換器的實現實例:
- 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;
- }
- 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);
- }
上面是ConvertTo的實現,如果轉換的目標類型是string,我將Scope的兩個屬性轉換成string類型,并且用一個“,”連接起來,這就是我們在屬性瀏覽器里看到的表現形式,如圖:
如果轉換的目標類型是實例描述器(InstanceDescriptor,它負責生成實例化的代碼),我們需要構造一個實例描述器,構造實例描述器的時候,我們要利用反射機制獲得Scope類的構造器信息,并在new的時候傳入Scope實例的兩個屬性值。實例描述器會為我們生成這樣的代碼:this.myListControl1.Scope = new CustomControlSample.Scope(10, 200);在最后不要忘記調用 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);
- }
- }
上面是ConvertFrom的代碼,由于系統能夠直接將實例描述器轉換為Scope類型,所以我們就沒有必要再寫代碼,我們只需要關注如何將String(在屬性瀏覽出現的屬性值的表達)類型的值轉換為Scope類型。沒有很復雜的轉換,只是將這個字符串以“,”分拆開,并串換為Int32類型,然后new一個Scope類的實例,將分拆后轉換的兩個整型值賦給Scope的實例,然后返回實例。在這段代碼里,我們要判斷一下用戶設定的屬性值是否有效。比如,如果用戶在Scope屬性那里輸入了“10200”,由于沒有輸入“,”,我們無法將屬性的值分拆為兩個字符串,也就無法進行下面的轉換,所以,我們要拋出一個異常,通知用戶重新輸入。
C#類型轉換器的實現的相關內容就向你介紹到這里,希望對你了解和學習C#類型轉換器的實現有所幫助。
【編輯推薦】