适配器配置的自定义类型转换器

与自定义编辑器一样,自定义类型转换器会替代其子级之一的 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);  
      }  
   }  
}  

另请参阅

自定义适配器配置设计器
适配器配置的自定义下拉列表编辑器
适配器配置的自定义模式对话框编辑器
适配器的高级配置组件