【问题标题】:c# how to implement type converterc#如何实现类型转换器
【发布时间】:2017-09-24 18:05:58
【问题描述】:

我正在努力在 C# 中实现一个简单的类型转换器。 我按照本指南https://msdn.microsoft.com/en-us/library/ayybcxe5.aspx

这是我的课:

public class TestClass: TypeConverter
{
        public string Property1{ get; set; }
        public int Property2 { get; set; }
        public TestClass(string p1, int p2)
        {
            Property1= p1;
            Property2 = p2;
        }
        public override bool CanConvertFrom(ITypeDescriptorContext context,
        Type sourceType)
        {
            if (sourceType == typeof(string)) {
                 return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context,
         CultureInfo culture, object value)
        {
              if (value is string) {
                    return new TestClass ("", Int32.Parse(value.ToString()));
              }
              return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string)) {
               return "___"
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
 }

我做以下TestClass ("", Int32.Parse(value.ToString()));,目前我只对"1231" -> new TestClass("", 1231)这样的案例感兴趣

这里是给我一个异常的代码;

TypeConverter converter=TypeDescriptor.GetConverter( typeof(TestClass));
Object lalala = converter.ConvertFromString("234");

这段代码抛出NotSupportedException,但我不明白为什么

【问题讨论】:

    标签: c# typeconverter


    【解决方案1】:

    提供的代码有点混乱,并且缺少一些重要的东西。下面是一个将自定义类 CrazyClass 从和 string 转换的实现。

    CrazyClassTypeConverter

    public class CrazyClassTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var casted = value as string;
            return casted != null
                ? new CrazyClass(casted.ToCharArray())
                : base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            var casted = value as CrazyClass;
            return destinationType == typeof (string) && casted != null
                ? String.Join("", casted.Charray)
                : base.ConvertTo(context, culture, value, destinationType);
        }
    }
    

    CrazyClass

    (注意这个类是用TypeConverterAttribute装饰的)

    [TypeConverter(typeof(CrazyClassTypeConverter))]
    public class CrazyClass
    {
        public char[] Charray { get; }
    
        public CrazyClass(char[] charray)
        {
            Charray = charray;
        }
    }
    

    用法

    var crazyClass = new CrazyClass(new [] {'T', 'e', 's', 't'});
    var converter = TypeDescriptor.GetConverter(typeof(CrazyClass));
    
    //this should provide you the string "Test"        
    var crazyClassToString = converter.ConvertToString(crazyClass); 
    
    //provides you an instance of CrazyClass with property Charray set to {'W', 'h', 'a', 't' } 
    var stringToCrazyClass = converter.ConvertFrom("What"); 
    

    【讨论】:

    • 您好,但是 stringToCrazyClass 是“对象”而不是 CrazyClass 类型如何获取它作为 CrazyClass 类型?
    • @alhpe 您只需要将其转换为 CrazyClass
    • char + array = charraay...你这个疯狂的 CrazyClass
    【解决方案2】:

    您必须将此转换器附加到具有TypeConverter 属性的类。
    TypeDescriptor.GetConverter 获取该类的附加转换器。

    你最好分班:

    [TypeConverter(typeof (TestClassConverter))]
    public class TestClass
    {
        public string Property1 { get; set; }
        public int Property2 { get; set; }
        public TestClass(string p1, int p2)
        {
            Property1 = p1;
            Property2 = p2;
        }
    }
    
    [TypeConverter(typeof (TestClassConverter))]
    public class TestClassConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context,
        Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context,
         CultureInfo culture, object value)
        {
            if (value is string)
            {
                return new TestClass("", Int32.Parse(value.ToString()));
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context,
        CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string)) { return "___"; }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    

    【讨论】:

    • 你为什么把TypeConverterAttribute放在类和转换器上?
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2019-07-16
    • 2015-05-18
    • 2013-02-01
    相关资源
    最近更新 更多