找不到 属性 的 TypeConverter

TypeConverter isn't found for property

这基本上是问题的延续:Typeconverter not working for single properties

原始问题没有标记为已回答,但它似乎是我项目中特定场景的前进方向,特别是因为我所有的阅读都表明它应该确实有效。我试了一下,但遇到了与最后评论中提到的相同的问题(没有断点被击中,或者在我的例子中,没有输出转储到结果 window)。这是我开始的内容,简化为满足我需要的基本示例†。

在 LINQPad 中

void Main()
{
    var prop = typeof(Request).GetProperty("Names");
    var converter = TypeDescriptor.GetConverter(prop.PropertyType.Dump()).Dump();
    var value = converter.ConvertFrom("One,Two").Dump();
}

public class Request
{
    [TypeConverter(typeof(EnumerableTypeConverter<string>))]
    public IEnumerable<string> Names { get; set; }
}

public class EnumerableTypeConverter<T> : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        new { Context = context, SourceType = sourceType }.Dump("CanConvertFrom");

        if (sourceType == typeof(string))
            return true;

        return false;
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        new { Context = context, DestinationType = destinationType }.Dump("CanConvertTo");

        return true;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        new { Context = context, Culture = culture, Value = value }.Dump("ConvertFrom");

        var source = value as string;

        if (source == null)
            return value.ToString();

        return source;
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        new { Context = context, Culture = culture, Value = value, DestinationType = destinationType }.Dump("ConvertTo");

        var s = value as IEnumerable<string>;

        return s != null ? string.Join(",", s) : base.ConvertFrom(context, culture, value);
    }

    public override bool IsValid(ITypeDescriptorContext context, object value)
    {
        new { Context = context, Value = value }.Dump("IsValid");

        return true;
    }
}

输出为

typeof(IEnumerable<String>)
System.Collections.Generic.IEnumerable`1[System.String]

ReferenceConverter
System.ComponentModel.ReferenceConverter

null

这表明问题是 1) 没有看到 TypeConverterAttribute 定义,2) 没有找到我的自定义 TypeConverter,或 3) 无法创建它默默地失败了。所有这些看起来都不太可能,但显然结果不会说谎。因此,我的理解有些不对劲。我直接实例化实例的时候,没有遇到任何异常。

var testConverter = new EnumerableTypeConverter<string>().Dump();

EnumerableTypeConverter<String>
UserQuery+EnumerableTypeConverter`1[System.String]

对于笑容和傻笑,我尝试了非通用方法

[TypeConverter(typeof(EnumerableStringConverter))]
public IEnumerable<string> Names { get; set; }

public class EnumerableStringConverter : TypeConverter
{
    // -- same implementation as EnumerableTypeConverter<T> --
}

但得到了同样的结果。我在这里错过了什么?


† 该代码将读取包含 属性 名称和关联值的文件。接下来它将通过名称查找匹配的 属性 并通过转换器设置值。一些实际值是 JSON 数组,但在创建将反序列化 JSON 数组返回匹配的 属性 的完整解决方案之前,我正在采取一些小步骤来验证我所做的事情。

您将转换器设置为 属性 因此您应该为这个 属性 而不是类型 IEnumerable<string> 获得转换器。

static void Main(string[] args)
{
    var properties = TypeDescriptor.GetProperties(typeof(Request));
    var propItem = properties["Names"];
    var converter = propItem.Converter;
    // converter has type {EnumerableTypeConverter<string>}
}