From 7e38dec8ffde97a9e271b75bc94f0cf3c485f240 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Thu, 14 Jan 2010 20:27:03 +1300 Subject: [PATCH] added FieldSaver --- OpenRa.FileFormats/FieldLoader.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/OpenRa.FileFormats/FieldLoader.cs b/OpenRa.FileFormats/FieldLoader.cs index ccd4efd2ef..0c0046df5b 100644 --- a/OpenRa.FileFormats/FieldLoader.cs +++ b/OpenRa.FileFormats/FieldLoader.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Reflection; namespace OpenRa.FileFormats { @@ -70,4 +71,23 @@ namespace OpenRa.FileFormats 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().Select(a => a.ToString()).ToArray()) + : v.ToString(); + } + } }