As an example, I would like to serialize and deserialize a System.Version
object as part of my application's custom configuration section. I am attempting to do so with the following property declaration:
public class ConfigElement : ConfigurationElement
{
[ConfigurationProperty("ver", IsRequired = false, DefaultValue = "1.2.4.8")]
public Version Ver
{
get { return (Version)this["ver"]; }
set { this["ver"] = value; }
}
}
Unfortunately, attempting to serialize or use this property (with or without the DefaultValue
) yields the following exception message.
System.Configuration.ConfigurationErrorsException : The value of the property 'ver' cannot be converted to string. The error is: Unable to find a converter that supports conversion to/from string for the property 'ver' of type 'Version'.
System.Version.ToString()
writes the object to a well-known string format which is consumable by System.Version.ctor(string)
, so it seems feasible for a "converter" to exist for this type. Comparably, the System.TimeSpan
type has similar methods and functions (Parse
in-place of .ctor(string)
) and the type works well with the configuration system (a converter must already exist).
How do I know if a type has a suitable converter? What contract (implicit or otherwise) must such a type satisfy?