Use available TypeConverters to serialize and deserialize types
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -374,6 +375,22 @@ namespace OpenRA
|
||||
return InvalidValueAction(value, fieldType, fieldName);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
var conv = TypeDescriptor.GetConverter(fieldType);
|
||||
if (conv.CanConvertFrom(typeof(string)))
|
||||
{
|
||||
try
|
||||
{
|
||||
return conv.ConvertFromInvariantString(value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return InvalidValueAction(value, fieldType, fieldName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UnknownFieldAction("[Type] {0}".F(value), fieldType);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -88,6 +89,19 @@ namespace OpenRA
|
||||
if (t == typeof(DateTime))
|
||||
return ((DateTime)v).ToString("yyyy-MM-dd HH-mm-ss", CultureInfo.InvariantCulture);
|
||||
|
||||
// Try the TypeConverter
|
||||
var conv = TypeDescriptor.GetConverter(t);
|
||||
if (conv.CanConvertTo(typeof(string)))
|
||||
{
|
||||
try
|
||||
{
|
||||
return conv.ConvertToInvariantString(v);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return v.ToString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user