19

我想TypeConverter为一个泛型类创建一个,如下所示:

[TypeConverter(typeof(WrapperConverter<T>))]
public class Wrapper<T> 
{

   public T Value 
   {
      // get & set 
   }

   // other methods

}


public class WrapperConverter<T> : TypeConverter<T>
{

   // only support To and From strings
   public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
   {
      if (sourceType == typeof(string))
      {
         return true;
      }
      return base.CanConvertFrom(context, sourceType);
   }

   public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
   {
      if (destinationType == typeof(string))
      {
         return true;
      }
      return base.CanConvertTo(context, destinationType);
   }

   public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
   {
      if (value is string)           
      {
         TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
         T inner = converter.ConvertTo(value, destinationType);
         return new Wrapper<T>(inner);
      }
      return base.ConvertFrom(context, culture, value);
   }

   public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
   {
      if (destinationType == typeof(System.String))
      {
         Wrapper<T> wrapper = value as Wrapper<T>();
         TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
         return converter.ConvertTo(wrapper.Value, destinationType);
      }   
      return base.ConvertTo(context, culture, value, destinationType);
   }
}

问题在于您不能在此行中使用泛型,这是不允许的:

[TypeConverter(typeof(WrapperConverter<T>))]
public class Wrapper<T> 

我的下一个方法是尝试定义一个可以处理任何Wrapper<T>实例的单个非泛型转换器。反射和泛型的混合让我难以理解如何同时实现ConvertTo方法ConvertFrom

例如,我的 ConvertTo 看起来像这样:

public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
   if (destinationType == typeof(System.String)           
       && value.GetType().IsGenericType)
   {

       // 1.  How do I enforce that value is a Wrapper<T> instance?

       Type innerType = value.GetType().GetGenericArguments()[0];

       TypeConverter converter = TypeDescriptor.GetConverter(innerType);

       // 2.  How do I get to the T Value property?  Introduce an interface that Wrapper<T> implements maybe?
       object innerValue = ??? 

       return converter.ConvertTo(innerValue, destinationType);


   }
   return base.ConvertTo(context, culture, value, destinationType);
}

我遇到ConvertFrom了最大的问题,因为我无法知道将传入的字符串转换为哪个 Wrapper 类。

我已经创建了几个用于 ASP.NET 4 Web API 框架的自定义类型和 TypeConverters,这也是我需要使用它的地方。

我尝试的另一件事是在运行时分配我的通用版本转换器,如此处所示但 WebAPI 框架不尊重它(这意味着从未创建转换器)。

最后一点,我使用的是 .NET 4.0 和 VS 2010。

4

2 回答 2

43

我通过创建一个可以处理从我的泛型类派生的所有类型的单个转换器解决了这个问题。通过捕获构造函数中的信息解决了了解 ConvertFrom 中的通用 arg T 的大问题,如下所示。

public MyGenericConverter(Type type)
{
    if (type.IsGenericType 
        && type.GetGenericTypeDefinition() == typeof(MyGenericClass<>)
        && type.GetGenericArguments().Length == 1)
    {
        _genericInstanceType = type;
        _innerType = type.GetGenericArguments()[0];
        _innerTypeConverter = TypeDescriptor.GetConverter(_innerType);            
    }
    else
    {
        throw new ArgumentException("Incompatible type", "type");
    }
}

我花了很长时间才发现 .NET 基础结构会反射性地调用此构造函数重载(如果已定义)。它不是记录在案的 TypeConverter 类的一部分。

希望这一切对下一个人有所帮助。

于 2013-02-20T13:16:27.470 回答
12

尽管@tcarvin 的回答非常有趣——它适用于 .NET Framework 4.6.1 并且从我在代码中看到的它也应该适用于 .NET Core,但有一个替代解决方案使用TypeDescriptionProviderAttribute它不依赖于该实现他描述的细节(构造函数接受类型的参数Type)。

有:

public class FooTypeConverter<T> : TypeConverter { ... }

public class FooTypeDescriptor : CustomTypeDescriptor
{
    private Type objectType;

    public FooTypeDescriptor(Type objectType)
    {
        this.objectType = objectType;
    }

    public override TypeConverter GetConverter()
    {
        var genericArg = objectType.GenericTypeArguments[0];
        var converterType = typeof(FooTypeConverter<>).MakeGenericType(genericArg);
        return (TypeConverter)Activator.CreateInstance(converterType);
    }
}

public class FooTypeDescriptionProvider : TypeDescriptionProvider
{
    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        return new FooTypeDescriptor(objectType);
    }
}

您只需要将 TypeDescriptionProviderAttribute 应用于目标类,例如:

[TypeDescriptionProvider(typeof(FooTypeDescriptionProvider))]
public class Foo<T> { }

进而

TypeDescriptor.GetConverter(typeof(Foo))将按预期工作。

于 2018-12-14T00:24:22.077 回答