WinForm控制項開發總結(六) 控制項屬性類型轉換器代碼詳解

來源:互聯網
上載者:User

在上一篇文章,我為控制項添加一個一個複雜屬性,並且為這個屬性的類型的編寫了一個類型轉換器, 現在我們來看看這個類型轉換器的代碼,並解釋一下這些代碼的意義。

要實現一個類型轉換器,我們必須要重寫(override)四個方法:

CanConvertFrom()――根據型別參數進行測試,判斷是否能從這個類型轉換成當前類型,在本例中我 們只提供轉換string和InstanceDescriptor類型的能力。

CanConvertTo()――根據型別參數進行測試,判斷是否能從當前類型轉換成指定的類型。

ConvertTo()――將參數value的值轉換為指定的類型。

ConvertFrom()――串換參數value,並返回但書類型的一個對象。

 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);        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.