Use reflection to get predefined layouts

This commit is contained in:
Yoshi Askharoun
2024-07-07 22:46:34 -05:00
parent e794bc9903
commit f94e29a368
+17 -19
View File
@@ -1,6 +1,6 @@
using Microsoft.Iris.Layouts;
using Microsoft.Iris.Render;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Iris.Layout;
@@ -8,16 +8,28 @@ public static class PredefinedLayouts
{
public static bool TryGetFromName(string name, out ILayout layout)
{
return s_NameToLayoutMap.TryGetValue(name.ToLowerInvariant(), out layout);
var prop = typeof(PredefinedLayouts).GetProperties()
.FirstOrDefault(p => p.Name.Equals(name, System.StringComparison.InvariantCultureIgnoreCase));
if (prop is null)
{
layout = null;
return false;
}
layout = (ILayout)prop.GetValue(null);
return true;
}
public static bool TryConvertToString(ILayout layout, out string name)
{
foreach (var kvp in s_NameToLayoutMap)
foreach (var prop in typeof(PredefinedLayouts).GetProperties())
{
if (kvp.Value.Equals(layout))
var value = prop.GetValue(null);
if (value == layout)
{
name = kvp.Key;
name = prop.Name;
return true;
}
}
@@ -46,18 +58,4 @@ public static class PredefinedLayouts
{
Orientation = Orientation.Vertical
};
private static readonly Dictionary<string, ILayout> s_NameToLayoutMap = new(10)
{
["anchor"] = Anchor,
["default"] = Default,
["dock"] = Dock,
["grid"] = Grid,
["scale"] = Scale,
["popup"] = Popup,
["stack"] = Stack,
["form"] = Form,
["horizontalflow"] = HorizontalFlow,
["verticalflow"] = VerticalFlow,
};
}