Question

I'm using some third party code which uses TypeConverters to "cast" objects to types specified as generic parameters.

The 3rd party code gets the string type converter, and expects to do all conversions through that e.g.

var typeConverter = TypeDescriptor.GetConverter(typeof(string));

I've written a custom type, and type converter for it (and registered it with the TypeDescriptor attribute) but it's not getting used by the 3rd party code, which fails on the call to typeConverter.CanConvertTo(MyCustomType)

Until today I'd only encountered TypeConverters in the abstract, I've seen mentions of them but never built or used one.

Has anyone any idea what I'm doing wrong here?

My - cut down - code

using System;
using System.ComponentModel;

namespace IoNoddy
{
[TypeConverter(typeof(TypeConverterForMyCustomType))]
public class MyCustomType
{
    public Guid Guid { get; private set; }
    public MyCustomType(Guid guid)
    {
        Guid = guid;
    }
    public static MyCustomType Parse(string value)
    {
        return new MyCustomType(Guid.Parse(value));
    }
}

public class TypeConverterForMyCustomType
    : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        string strValue;
        if ((strValue = value as string) != null)
            try
            {
                return MyCustomType.Parse(strValue);
            }
            catch (FormatException ex)
            {
                throw new FormatException(string.Format("ConvertInvalidPrimitive: Could not convert {0} to MyCustomType", value), ex);
            }
        return base.ConvertFrom(context, culture, value);
    }
}
}

static void Main(string[] args)
{
    // Analogous to what 3rd party code is doing: 
    var typeConverter = TypeDescriptor.GetConverter(typeof(string));
    // writes "Am I convertible? false"
    Console.WriteLine("Am I convertible? {0}",  typeConverter.CanConvertTo(typeof(MyCustomType))); 
}
Was it helpful?

Solution

You check CanConvertTo so add to yours converter:

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType == typeof(MyCustomType)) || base.CanConvertTo(context, destinationType);
    }

to some where:

    public static void Register<T, TC>() where TC : TypeConverter
    {
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(T), attr);
    }   

and to main:

Register<string, TypeConverterForMyCustomType>();            
var typeConverter = TypeDescriptor.GetConverter(typeof(string));

yours sample shuld work after that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top