From 6d9e0a036c6b535c2497b9f84203ef046a8f2b41 Mon Sep 17 00:00:00 2001 From: Isak Naslund Date: Tue, 17 Mar 2020 14:50:28 +0100 Subject: [PATCH 1/2] Adding descriptions of enum options --- Reflection_Engine/Query/Description.cs | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Reflection_Engine/Query/Description.cs b/Reflection_Engine/Query/Description.cs index 9bdcfd425..0b9b8809c 100644 --- a/Reflection_Engine/Query/Description.cs +++ b/Reflection_Engine/Query/Description.cs @@ -108,6 +108,7 @@ public static string Description(this Type type) } /***************************************************/ + [Description("Return the custom description of a C# class")] public static string Description(this Type type, QuantityAttribute quantityAttribute) { @@ -120,7 +121,6 @@ public static string Description(this Type type, QuantityAttribute quantityAttri string desc = ""; - //If a quantity attribute is present, this is used to generate the default description if (quantityAttribute != null) { @@ -137,6 +137,11 @@ public static string Description(this Type type, QuantityAttribute quantityAttri desc += ":" + Environment.NewLine; desc += attribute.Description; } + + //If type is enum, list options with descriptions + if (type.IsEnum) + desc += EnumItemDescription(type); + Type innerType = type; while (typeof(IEnumerable).IsAssignableFrom(innerType) && innerType.IsGenericType) @@ -180,6 +185,33 @@ public static string IDescription(this object item) return ""; } + /***************************************************/ + /**** Private Methods ****/ + /***************************************************/ + + [Description("Lists all the enum options and their descriptions.")] + private static string EnumItemDescription(this Type type) + { + FieldInfo[] fields = type.GetFields(); + string desc = Environment.NewLine + "Enum values:"; + foreach (var field in fields) + { + //Skip the value option + if (field.Name == "value__") + continue; + + desc += Environment.NewLine; + desc += "-" + field.Name; + + DescriptionAttribute attribute = field.GetCustomAttribute(); + + if (attribute != null) + desc += ": " + attribute.Description; + } + + return desc; + } + /***************************************************/ } } From d3ec9a6afcc9869aabd6e98e36bb229203851c06 Mon Sep 17 00:00:00 2001 From: Isak Naslund Date: Tue, 17 Mar 2020 17:13:22 +0100 Subject: [PATCH 2/2] Adding cutofflimit for enums --- Reflection_Engine/Query/Description.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Reflection_Engine/Query/Description.cs b/Reflection_Engine/Query/Description.cs index 0b9b8809c..420f760d5 100644 --- a/Reflection_Engine/Query/Description.cs +++ b/Reflection_Engine/Query/Description.cs @@ -194,8 +194,13 @@ private static string EnumItemDescription(this Type type) { FieldInfo[] fields = type.GetFields(); string desc = Environment.NewLine + "Enum values:"; - foreach (var field in fields) + + int m = Math.Min(fields.Length, 20); + + for (int i = 0; i < m; i++) { + FieldInfo field = fields[i]; + //Skip the value option if (field.Name == "value__") continue; @@ -209,6 +214,9 @@ private static string EnumItemDescription(this Type type) desc += ": " + attribute.Description; } + if (fields.Length > m) + desc += Environment.NewLine + "-...And more"; + return desc; }