C#實(shí)例詳解TypeConverterAttribute應(yīng)用
TypeConverterAttribute應(yīng)用是如何實(shí)現(xiàn)的呢?那么這里向你介紹在C# WinForm控件開發(fā)中是如何操作的(C#實(shí)例詳解),希望對你了解TypeConverterAttribute應(yīng)用有所幫助。
C#實(shí)例詳解TypeConverterAttribute應(yīng)用在創(chuàng)建的控件代碼中添加一個(gè)Scope屬性:
- [Browsable(true)]
- public Scope Scope
- {
- get
- {
- return _scope;
- }
- set
- {
- _scope = value;
- }
- }
這個(gè)屬性的類型是Scope類,代碼如下:
- 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控件工程,然后在測試的工程里選中添加的控件,然后在屬性瀏覽器里觀察它的屬性,發(fā)現(xiàn)Scope屬性是灰的,不能編輯。前一篇文章提到了,在屬性瀏覽器里可以編輯的屬性都是有類型轉(zhuǎn)換器的,而.NET框架為基本的類型和常用的類型都提供了默認(rèn)的類型轉(zhuǎn)換器。接下來我們?yōu)镾cope類添加一個(gè)類型轉(zhuǎn)換器,以便這個(gè)屬性能夠被編輯,而且也可以在源代碼文件里自動(dòng)生成相應(yīng)的代碼。下面是類型轉(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)轭愋吞峁╊愋娃D(zhuǎn)換器,我們在類型前面添加一個(gè)TypeConverterAttribute,如下:
- [TypeConverter(typeof(ScopeConverter))]
- public class Scope
添加完以后build工程,然后切換到測試工程,選中控件,在屬性瀏覽器里查看屬性,現(xiàn)在的Scope屬性可以編輯了,如下圖所示:
我們修改默認(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類的類型轉(zhuǎn)換器為屬性提供了實(shí)例化的代碼。
C#實(shí)例詳解TypeConverterAttribute應(yīng)用的相關(guān)內(nèi)容就向你介紹到這里,希望那個(gè)對你了解和學(xué)習(xí)C#實(shí)例詳解TypeConverterAttribute應(yīng)用有所幫助。