zcz527 / autofac

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

TypeManipulation.ChangeToCompatibleType should use Parse method if available #446

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Many of the .Net types follow a pattern where they support static 
Parse/TryParse methods. These are especially useful when coming from XML. 
Picture the situation where you want to set an IPAddress property from the 
config file. This situation is not supported out of the box, but it could be 
easily supported with this modification to 
TypeManipulation.ChangeToCompatibleType:

// existing code:
if (destinationType.IsAssignableFrom(value.GetType()))
    return value;

// new code:
if (value is string)
{
    // see if there is a parse method
    var parser = destinationType.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public);
    if (parser != null)
    {
        var parameters = new object[] {value, null};
        if ((bool) parser.Invoke(null, parameters))
            return parameters[1];
    }
}

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

GoogleCodeExporter commented 8 years ago

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

GoogleCodeExporter commented 8 years ago

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

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 5d9e3b6a13a7.

Original comment by alex.meyergleaves on 2 Jul 2013 at 1:31