1

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))); 
}
5
  • msdn.microsoft.com/en-us/library/ayybcxe5.aspx good place to start reading hope it helps Commented Feb 21, 2013 at 15:50
  • May be you shuld register your converter? Commented Feb 21, 2013 at 15:51
  • @gabba: Care to elaborate? or should I just sit here beating myself with the stoopid stick? Commented Feb 21, 2013 at 16:03
  • @Binary Worrier I just give you an idea what you doing wrong. Try to realize how 3rd party code get to know about your type and converter related with this type? Commented Feb 21, 2013 at 16:50
  • @gabba: It's the stoopid stick so? Commented Feb 21, 2013 at 16:53

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

you are a gentle(wo)man and I apologise for any ill I may have wished upon you yesterday ... honestly I do hope the boils clear up quickly :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.