From 61be13c5396c0a1dce80e1de9c3489941e59d1e8 Mon Sep 17 00:00:00 2001 From: adecler Date: Thu, 20 Oct 2022 14:12:54 +0100 Subject: [PATCH 1/3] Add DisplayText support on enums --- BHoM_Engine/Compute/ParseEnum.cs | 6 ++++-- BHoM_Engine/Convert/ToText.cs | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/BHoM_Engine/Compute/ParseEnum.cs b/BHoM_Engine/Compute/ParseEnum.cs index 853b80204..960f0f946 100644 --- a/BHoM_Engine/Compute/ParseEnum.cs +++ b/BHoM_Engine/Compute/ParseEnum.cs @@ -64,9 +64,11 @@ public static object ParseEnum(Type enumType, string value) return Enum.GetValues(enumType).OfType() .FirstOrDefault(x => { FieldInfo fi = enumType.GetField(x.ToString()); - DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; + DisplayTextAttribute[] displayTexts = fi.GetCustomAttributes(typeof(DisplayTextAttribute), false) as DisplayTextAttribute[]; + DescriptionAttribute[] descriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; - return attributes != null && attributes.Count() > 0 && attributes.First().Description == value; + return (displayTexts != null && displayTexts.Count() > 0 && displayTexts.First().Text == value) + || (descriptions != null && descriptions.Count() > 0 && descriptions.First().Description == value); }); } } diff --git a/BHoM_Engine/Convert/ToText.cs b/BHoM_Engine/Convert/ToText.cs index 544330916..fc89b3201 100644 --- a/BHoM_Engine/Convert/ToText.cs +++ b/BHoM_Engine/Convert/ToText.cs @@ -22,6 +22,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; @@ -158,7 +159,29 @@ public static string ToText(this Type type, bool includePath = false, bool repla } } + /***************************************************/ + + public static string ToText(Enum item, bool includePath = false) + { + if (item == null) + return "null"; + + if (includePath) + { + Type type = item.GetType(); + return type.Path() + "." + type.Name + "." + item.ToString(); + } + else + { + FieldInfo fi = item.GetType().GetField(item.ToString()); + DisplayTextAttribute[] attributes = fi.GetCustomAttributes(typeof(DisplayTextAttribute), false) as DisplayTextAttribute[]; + if (attributes != null && attributes.Count() > 0) + return attributes.First().Text; + else + return item.ToString(); + } + } /***************************************************/ /**** Fallback Methods ****/ @@ -168,7 +191,7 @@ private static string ToText(object item, bool includePath = false) { if (item == null) return "null"; - else if (item is string || item.GetType().IsEnum) + else if (item is string) return item.ToString(); else return item.GetType().ToString(); From 78c0831d206680f48c38e315b794c1120a013784 Mon Sep 17 00:00:00 2001 From: adecler Date: Thu, 20 Oct 2022 14:57:17 +0100 Subject: [PATCH 2/3] adding missing this on ToText method --- BHoM_Engine/Convert/ToText.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BHoM_Engine/Convert/ToText.cs b/BHoM_Engine/Convert/ToText.cs index fc89b3201..97afa877a 100644 --- a/BHoM_Engine/Convert/ToText.cs +++ b/BHoM_Engine/Convert/ToText.cs @@ -161,7 +161,7 @@ public static string ToText(this Type type, bool includePath = false, bool repla /***************************************************/ - public static string ToText(Enum item, bool includePath = false) + public static string ToText(this Enum item, bool includePath = false) { if (item == null) return "null"; From 71f4a9f0423b8302c11228fe84bf5e3cb64587bb Mon Sep 17 00:00:00 2001 From: adecler Date: Thu, 20 Oct 2022 15:13:59 +0100 Subject: [PATCH 3/3] Adding descriptions for ToText methods --- BHoM_Engine/Convert/ToText.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/BHoM_Engine/Convert/ToText.cs b/BHoM_Engine/Convert/ToText.cs index 97afa877a..2f07cdb8a 100644 --- a/BHoM_Engine/Convert/ToText.cs +++ b/BHoM_Engine/Convert/ToText.cs @@ -37,6 +37,10 @@ public static partial class Convert /**** Interface Methods ****/ /***************************************************/ + [Description("Provides a human-friendly text representation of the provided object.")] + [Input("member", "object to convert to text.")] + [Input("includePath", "If true, the path/namespace will be provided.")] + [Output("Text representation.")] public static string IToText(this object member, bool includePath = false) { if (member == null) @@ -50,6 +54,10 @@ public static string IToText(this object member, bool includePath = false) /**** Public Methods ****/ /***************************************************/ + [Description("Provides a human-friendly text representation of a member of a class.")] + [Input("member", "member to convert to text.")] + [Input("includePath", "If true, the path/namespace will be provided.")] + [Output("Text representation.")] public static string ToText(this MemberInfo member, bool includePath = false) { if (member == null) @@ -65,6 +73,18 @@ public static string ToText(this MemberInfo member, bool includePath = false) /***************************************************/ + [Description("Provides a human-friendly text representation of a method.")] + [Input("method", "method to convert to text.")] + [Input("includePath", "If true, the path/namespace will be provided.")] + [Input("paramStart", "Start symbol used for the beginning of the method parameters. Usually, '('.")] + [Input("paramSeparator", "Symbol used to separate the parameters. Usually, ','.")] + [Input("paramEnd", "Start symbol used for the end of the method parameters. Usually, ')'.")] + [Input("removeIForInterface", "If true, the 'I' in front of the interface method will not show.")] + [Input("includeParamNames", "If true, the name of the parameters will be written. Otherwise, only their types will show.")] + [Input("maxParams", "Maximum number of parameter to write. Any remaining will be represented with '...'.")] + [Input("maxChars", "Maximum number of characters to use. Any remaining will be represented with '...'.")] + [Input("includeParamPaths", "If true, the path/namespace will be provided.")] + [Output("Text representation.")] public static string ToText(this MethodBase method, bool includePath = false, string paramStart = "(", string paramSeparator = ", ", string paramEnd = ")", bool removeIForInterface = true, bool includeParamNames = true, int maxParams = 5, int maxChars = 40, bool includeParamPaths = false) { if (method == null) @@ -124,6 +144,13 @@ public static string ToText(this MethodBase method, bool includePath = false, st /***************************************************/ + [Description("Provides a human-friendly text representation of a type.")] + [Input("type", "type to convert to text.")] + [Input("includePath", "If true, the path/namespace will be provided.")] + [Input("genericStart", "Start symbol used for the beginning of the generic parameters, if any. Usually, '<'.")] + [Input("genericSeparator", "Symbol used to separate the generic parameters. Usually, ','.")] + [Input("genericEnd", "Start symbol used for the end of the generic parameters, if any. Usually, '>'.")] + [Output("Text representation.")] public static string ToText(this Type type, bool includePath = false, bool replaceGeneric = false, string genericStart = "<", string genericSeparator = ", ", string genericEnd = ">") { if (type == null) @@ -161,6 +188,10 @@ public static string ToText(this Type type, bool includePath = false, bool repla /***************************************************/ + [Description("Provides a human-friendly text representation of an enum.")] + [Input("item", "enum to convert to text.")] + [Input("includePath", "If true, the path/namespace will be provided.")] + [Output("Text representation.")] public static string ToText(this Enum item, bool includePath = false) { if (item == null)