From 8fab0353843a519a1f3daf403a3bb2f5604a6d4f Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 17 Feb 2024 23:44:49 +1100 Subject: [PATCH] remove extra expression from enum humanize --- src/Humanizer/EnumHumanizeExtensions.cs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Humanizer/EnumHumanizeExtensions.cs b/src/Humanizer/EnumHumanizeExtensions.cs index 36c4ddc0c..6198c9c74 100644 --- a/src/Humanizer/EnumHumanizeExtensions.cs +++ b/src/Humanizer/EnumHumanizeExtensions.cs @@ -19,23 +19,20 @@ public static string Humanize(this Enum input) { return Enum.GetValues(type) .Cast() - .Where(e => e.CompareTo(Convert.ChangeType(Enum.ToObject(type, 0), type)) != 0) - .Where(input.HasFlag) - .Select(e => e.Humanize()) + .Where(_ => input.HasFlag(_) && + _.CompareTo(Convert.ChangeType(Enum.ToObject(type, 0), type)) != 0) + .Select(_ => _.Humanize()) .Humanize(); } var caseName = input.ToString(); - var member = type.GetTypeInfo().GetDeclaredField(caseName); + var member = type.GetField(caseName)!; - if (member != null) - { - var description = GetCustomDescription(member); + var description = GetCustomDescription(member); - if (description != null) - { - return description; - } + if (description != null) + { + return description; } return caseName.Humanize(); @@ -48,9 +45,9 @@ public static string Humanize(this Enum input) static bool IsBitFieldEnum(Type type) => type.GetCustomAttribute(typeof(FlagsAttribute)) != null; - static string GetCustomDescription(MemberInfo memberInfo) + static string GetCustomDescription(MemberInfo member) { - var displayAttribute = memberInfo.GetCustomAttribute(); + var displayAttribute = member.GetCustomAttribute(); if (displayAttribute != null) { var description = displayAttribute.GetDescription(); @@ -62,7 +59,7 @@ static string GetCustomDescription(MemberInfo memberInfo) return displayAttribute.GetName(); } - foreach (var attr in memberInfo.GetCustomAttributes()) + foreach (var attr in member.GetCustomAttributes()) { var attrType = attr.GetType(); var descriptionProperty =