added FieldSaver

This commit is contained in:
Chris Forbes
2010-01-14 20:27:03 +13:00
parent c338876b34
commit 7e38dec8ff

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Reflection;
namespace OpenRa.FileFormats namespace OpenRa.FileFormats
{ {
@@ -70,4 +71,23 @@ namespace OpenRa.FileFormats
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
} }
public static class FieldSaver
{
public static MiniYaml Save(object o)
{
return new MiniYaml(null, o.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)
.ToDictionary(
f => f.Name,
f => new MiniYaml(FormatValue(o, f))));
}
static string FormatValue(object o, FieldInfo f)
{
var v = f.GetValue(o);
return f.FieldType.IsArray
? string.Join(",", ((Array)v).OfType<object>().Select(a => a.ToString()).ToArray())
: v.ToString();
}
}
} }