What is the implicit contract for a property's type when used with the ConfigurationProperty attribute?
See the question and my original answer on StackOverflowFor the ConfigurationProperty to work, the type used must be associated with a TypeConverter than knows how to convert from a string. ConfigurationProperty does have a Converter property, but alas, it's read-only. And, that's really bad luck, Version does not have an implicit TypeConverter declared either.
What you can do though, is add a TypeConverterAttribute to the Version class programmatically, and it will work around all these issues. So you need to basically call this line once in your program before accessing the configuration:
TypeDescriptor.AddAttributes(typeof(Version), new TypeConverterAttribute(typeof(VersionTypeConverter)));
// ... you can call configuration code now...
with the following custom-made VersionTypeConverter:
public class VersionTypeConverter : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return new Version((string)value);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
}