Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

TypeConverters on Properties are not used #445

Closed
alexmg opened this issue Jan 22, 2014 · 5 comments
Closed

TypeConverters on Properties are not used #445

alexmg opened this issue Jan 22, 2014 · 5 comments

Comments

@alexmg
Copy link
Member

alexmg commented Jan 22, 2014

From countpri...@gmail.com on June 21, 2013 08:14:19

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())
{
if (converter.CanConvertTo(pi.ParameterType))
return converter.ConvertTo(value, pi.ParameterType);
}
}
return TypeManipulation.ChangeToCompatibleType(value, pi.ParameterType);
});

Original issue: http://code.google.com/p/autofac/issues/detail?id=445

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on June 21, 2013 07:28:00

Labels: -Type-Defect Type-Enhancement Module-Configuration

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From countpri...@gmail.com on June 21, 2013 08:15:43

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.)

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on August 12, 2013 08:09:36

Status: Started
Owner: travis.illig

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on August 12, 2013 10:31:05

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.

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on August 12, 2013 10:37:44

This issue was closed by revision 20711b9288fd .

Status: Fixed

@alexmg alexmg closed this as completed Jan 22, 2014
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant