TypeConvert Demo
下班了,先把代码贴上
Code
[TypeConverter(typeof(AddressConverter))]
public class Address
{
public Address() { }
public Address(string street, string city,string state, string zip)
{
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
private String street = null;
private String city = null;
private String state = null;
private String zip = null;
[NotifyParentProperty(true)]
public String Street
{
get
{
return street;
}
set
{
street = value;
}
}
[NotifyParentProperty(true)]
[TypeConverter(typeof(CityConverter))]
public String City
{
get
{
return city;
}
set
{
city = value;
}
}
[NotifyParentProperty(true)]
public String State
{
get
{
return state;
}
set
{
state = value;
}
}
[NotifyParentProperty(true)]
public String Zip
{
get
{
return zip;
}
set
{
zip = value;
}
}
public override string ToString()
{
return this.ToString(CultureInfo.CurrentCulture);
}
protected virtual string ToString(CultureInfo culture)
{
return TypeDescriptor.GetConverter(typeof(Address)).ConvertToString(null,culture,this);
}
}
/**//// <summary>
/// 自定义类型转换器
/// </summary>
public class AddressConverter : ExpandableObjectConverter
{
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="sourceType">sourceType表示要转换的类型</param>
/// <returns>返回值能否将String类型转换为Address类型</returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType==typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="destinationType">表示要转换到的类型</param>
/// <returns>返回值能否将Address类型转换为String类型</returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value">value为要转换的类型</param>
/// <returns>将String类型转换为Address类型</returns>
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value == null)
return new Address();
if (value is string)
{
string s = (string)value;
if (s.Length == 0)
return new Address();
string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
if (parts.Length != 4)
throw new ArgumentException();
TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
return new Address((string)stringConverter.ConvertFromString(context,culture,parts[0]),
(string)stringConverter.ConvertFromString(context,culture,parts[1]),
(string)stringConverter.ConvertFromString(context,culture,parts[2]),
(string)stringConverter.ConvertFromString(context,culture,parts[3])
);
}
return base.ConvertFrom(context, culture, value);
}
/**//// <summary>
/// 将Address类型转换为String类型
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value">value为要转换的类型</param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value != null)
{
if (!(value is Address))
{
throw new ArgumentException();
}
}
if(destinationType==typeof(string))
{
if (value == null)
return string.Empty;
Address ad = (Address)value;
TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
return string.Join(culture.TextInfo.ListSeparator,
new string[] {
stringConverter.ConvertToString(context,culture,ad.State),
stringConverter.ConvertToString(context,culture,ad.City),
stringConverter.ConvertToString(context,culture,ad.Street),
stringConverter.ConvertToString(context,culture,ad.Zip),
});
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
/**//// <summary>
///
/// </summary>
public class CityConverter : StringConverter
{
/**//// <summary>
/// 返回此对象是否支持可以从列表中选取的标准值集
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
/**//// <summary>
/// 返回下拉框集合类
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[]
{
"naxx",
"mx",
"bwl"
});
}
/**//// <summary>
/// 标准值的集合是否为独占列表
/// 默认为flase,为true则表示无法修改列表值
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;//false可自定义内容 true只能选择下拉列表内容
}
}
[TypeConverter(typeof(AddressConverter))]
public class Address
{
public Address() { }
public Address(string street, string city,string state, string zip)
{
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
private String street = null;
private String city = null;
private String state = null;
private String zip = null;
[NotifyParentProperty(true)]
public String Street
{
get
{
return street;
}
set
{
street = value;
}
}
[NotifyParentProperty(true)]
[TypeConverter(typeof(CityConverter))]
public String City
{
get
{
return city;
}
set
{
city = value;
}
}
[NotifyParentProperty(true)]
public String State
{
get
{
return state;
}
set
{
state = value;
}
}
[NotifyParentProperty(true)]
public String Zip
{
get
{
return zip;
}
set
{
zip = value;
}
}
public override string ToString()
{
return this.ToString(CultureInfo.CurrentCulture);
}
protected virtual string ToString(CultureInfo culture)
{
return TypeDescriptor.GetConverter(typeof(Address)).ConvertToString(null,culture,this);
}
}
/**//// <summary>
/// 自定义类型转换器
/// </summary>
public class AddressConverter : ExpandableObjectConverter
{
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="sourceType">sourceType表示要转换的类型</param>
/// <returns>返回值能否将String类型转换为Address类型</returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType==typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="destinationType">表示要转换到的类型</param>
/// <returns>返回值能否将Address类型转换为String类型</returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
/**//// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value">value为要转换的类型</param>
/// <returns>将String类型转换为Address类型</returns>
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value == null)
return new Address();
if (value is string)
{
string s = (string)value;
if (s.Length == 0)
return new Address();
string[] parts = s.Split(culture.TextInfo.ListSeparator[0]);
if (parts.Length != 4)
throw new ArgumentException();
TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
return new Address((string)stringConverter.ConvertFromString(context,culture,parts[0]),
(string)stringConverter.ConvertFromString(context,culture,parts[1]),
(string)stringConverter.ConvertFromString(context,culture,parts[2]),
(string)stringConverter.ConvertFromString(context,culture,parts[3])
);
}
return base.ConvertFrom(context, culture, value);
}
/**//// <summary>
/// 将Address类型转换为String类型
/// </summary>
/// <param name="context"></param>
/// <param name="culture"></param>
/// <param name="value">value为要转换的类型</param>
/// <param name="destinationType"></param>
/// <returns></returns>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value != null)
{
if (!(value is Address))
{
throw new ArgumentException();
}
}
if(destinationType==typeof(string))
{
if (value == null)
return string.Empty;
Address ad = (Address)value;
TypeConverter stringConverter = TypeDescriptor.GetConverter(typeof(string));
return string.Join(culture.TextInfo.ListSeparator,
new string[] {
stringConverter.ConvertToString(context,culture,ad.State),
stringConverter.ConvertToString(context,culture,ad.City),
stringConverter.ConvertToString(context,culture,ad.Street),
stringConverter.ConvertToString(context,culture,ad.Zip),
});
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
/**//// <summary>
///
/// </summary>
public class CityConverter : StringConverter
{
/**//// <summary>
/// 返回此对象是否支持可以从列表中选取的标准值集
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
/**//// <summary>
/// 返回下拉框集合类
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[]
{
"naxx",
"mx",
"bwl"
});
}
/**//// <summary>
/// 标准值的集合是否为独占列表
/// 默认为flase,为true则表示无法修改列表值
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;//false可自定义内容 true只能选择下拉列表内容
}
}