Hi Marc
Sorry for the delay (I was busy writing APIs). Thanks for pointing out the
typo!
I decided to go with the XmlConvert because of the locale independence as I
couldn't find any reason to reproduce the implementation in that class just
to save a few method calls. I would have liked a generic
XmlConvert.ToString(object) signature, but I guess the implementation I came
up with is quite good in terms of maintenance and performance (the latter is
great).
Of course, another approach would be to use XmlSerializer or
DataContractSerializer, which will cope with more scenarios than I
care to have to list.
Again, the PropertyManager was intended as a storage for generic
configuration values / list of values. Not entire serialized graphs
(although it's definately supported by means of using a string configuration
value).
Here's the "CmsXmlConverter" class I ended up implementing - it may be of
use to others. As can be read from above, I'd have liked an
XmlConvert.ToString(object) method, but decided to skip the reflection
(emit) approch and wrote a switched implementation instead (which works very
well, I might add).
If you've got additional comments, I'm always very interested - and thanks
for the information so far!
public static string GetString(string typeName, object value)
{
if (value == null) { return null; }
switch (typeName)
{
case "System.String": { return (string) value; }
case "System.Guid": { return XmlConvert.ToString((Guid) value); }
case "System.DateTime": { return XmlConvert.ToString((DateTime) value,
XmlDateTimeSerializationMode.Utc); }
case "System.DateTimeOffset": { return
XmlConvert.ToString((DateTimeOffset) value); }
case "System.TimeSpan": { return XmlConvert.ToString((TimeSpan)
value); }
case "System.Bool": { return XmlConvert.ToString((bool) value); }
case "System.Byte": { return XmlConvert.ToString((byte) value); }
case "System.Char": { return XmlConvert.ToString((char) value); }
case "System.Decimal": { return XmlConvert.ToString((decimal) value); }
case "System.Double": { return XmlConvert.ToString((double) value); }
case "System.Single": { return XmlConvert.ToString((float) value); }
case "System.Int16": { return XmlConvert.ToString((short) value); }
case "System.Int32": { return XmlConvert.ToString((int) value); }
case "System.Int64": { return XmlConvert.ToString((long) value); }
case "System.SByte": { return XmlConvert.ToString((sbyte) value); }
case "System.UInt16": { return XmlConvert.ToString((ushort) value); }
case "System.UInt32": { return XmlConvert.ToString((uint) value); }
case "System.UInt64": { return XmlConvert.ToString((ulong) value); }
default: { return value.ToString(); }
}
}
public static object GetObject(string typeName, string value)
{
if (string.IsNullOrEmpty(value)) { return null; }
return GetParser(typeName)(value);
}
public static Func<string, object> GetParser(Type type)
{
return GetParser(type.FullName);
}
public static Func<string, object> GetParser(string typeName)
{
switch (typeName)
{
case "System.String": { return (p) => p; }
case "System.Guid": { return (p) => XmlConvert.ToGuid(p); }
case "System.DateTime": { return (p) => XmlConvert.ToDateTime(p,
XmlDateTimeSerializationMode.Utc); }
case "System.DateTimeOffset": { return (p) =>
XmlConvert.ToDateTimeOffset(p); }
case "System.TimeSpan": { return (p) => XmlConvert.ToTimeSpan(p); }
case "System.Bool": { return (p) => XmlConvert.ToBoolean(p); }
case "System.Byte": { return (p) => XmlConvert.ToByte(p); }
case "System.Char": { return (p) => XmlConvert.ToChar(p); }
case "System.Decimal": { return (p) => XmlConvert.ToDecimal(p); }
case "System.Double": { return (p) => XmlConvert.ToDouble(p); }
case "System.Single": { return (p) => XmlConvert.ToSingle(p); }
case "System.Int16": { return (p) => XmlConvert.ToInt16(p); }
case "System.Int32": { return (p) => XmlConvert.ToInt32(p); }
case "System.Int64": { return (p) => XmlConvert.ToInt64(p); }
case "System.SByte": { return (p) => XmlConvert.ToSByte(p); }
case "System.UInt16": { return (p) => XmlConvert.ToUInt16(p); }
case "System.UInt32": { return (p) => XmlConvert.ToUInt32(p); }
case "System.UInt64": { return (p) => Convert.ToUInt64(p); }
default:
{
Type type = Type.GetType(typeName);
MethodInfo info = type.GetMethod("Parse", new Type[] {
typeof(string) });
if (info == null) { throw new
InvalidOperationException(string.Format("Unable to find the Parse(string
value) method for type: {0}.", type.FullName)); }
return (p) => info.Invoke(null, new object[] { p });
}
}
}