zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

TypeConverters on Properties are not used #445

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The TypeManipulation.ChangeToCompatibleType call looks for a TypeConverter 
defined on the target data type. However, it is also possible to define them on 
properties directly. This is useful for built-in .NET types (like IPAddress) 
where you want a to/from string converter. To use these converters, modify the 
PropertyElementCollection to look like this:

(pi, c) =>
{
    var value = localParameter.CoerceValue();
    PropertyInfo prop;
    if (pi.TryGetDeclaringProperty(out prop))
    {
        foreach (var converter in prop.GetCustomAttributes(typeof(TypeConverter), false).Cast<TypeConverter>())
        {
            if (converter.CanConvertTo(pi.ParameterType))
                return converter.ConvertTo(value, pi.ParameterType);
        }
    }
    return TypeManipulation.ChangeToCompatibleType(value, pi.ParameterType);
});

Original issue reported on code.google.com by countpri...@gmail.com on 20 Jun 2013 at 10:14

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 21 Jun 2013 at 2:28

GoogleCodeExporter commented 8 years ago
It should be TypeConverterAttribute, not TypeConverter in the sample code. Then 
get the TypeConverter from the attribute. (I'm not aware of a TypeDescriptor 
helper that will get the TypeConverter for a property.)

Original comment by countpri...@gmail.com on 21 Jun 2013 at 3:15

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 12 Aug 2013 at 3:09

GoogleCodeExporter commented 8 years ago
Added support for parameters and properties to provide specific TypeConverter 
information using TypeConverterAttribute. Due to the way the attribute works, 
when using list or dictionary properties in XML, the specific TypeConverter 
implementation needs to actually convert the full list or dictionary from the 
ListElementCollection or DictionaryElementCollection (respectively), not just 
the element type. Examples of all of these can be seen in the unit tests.

I'll push the changes shortly.

Original comment by travis.illig on 12 Aug 2013 at 5:31

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 20711b9288fd.

Original comment by travis.illig on 12 Aug 2013 at 5:37