适配器配置的自定义类型转换器
与自定义编辑器一样,自定义类型转换器会替代其子级之一的 System.ComponentModel.TypeConverter 类。 转换器在此将格式添加到要保留但不会显示在属性页中的值。 ConvertFrom 方法在字符串值周围添加方括号,ConvertTo 方法会删除它们。
以下代码是自定义类型转换器的类定义:
using System;
using System.ComponentModel;
namespace AdapterManagement.ComponentModel {
public class DesignerTypeConverter : StringConverter {
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
return (typeof(String) == destinationType) || base.CanConvertTo (context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
if (typeof(String) == destinationType && value is String) {
return ((String)value).TrimStart('[').TrimEnd(']');
}
return base.ConvertTo (context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
return (typeof(String) == sourceType) || base.CanConvertFrom (context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
if (value is String) {
return "["+(String)value+"]";
}
return base.ConvertFrom (context, culture, value);
}
}
}