From 19c17029844311db07185ce780dd67bfd8a95d2a Mon Sep 17 00:00:00 2001 From: DomCR Date: Fri, 5 Jan 2024 13:41:25 +0100 Subject: [PATCH 1/4] parse --- CSUtilities.Tests/EnumUtilsTests.cs | 17 ------ .../Extensions/EnumExtensionsTests.cs | 56 +++++++++++++++++++ .../Mock/MockEnumWithStringValues.cs | 18 ------ .../Attributes/StringValueAttribute.cs | 2 - CSUtilities/Extensions/EnumExtensions.cs | 28 +++++++++- 5 files changed, 82 insertions(+), 39 deletions(-) delete mode 100644 CSUtilities.Tests/EnumUtilsTests.cs create mode 100644 CSUtilities.Tests/Extensions/EnumExtensionsTests.cs delete mode 100644 CSUtilities.Tests/Mock/MockEnumWithStringValues.cs diff --git a/CSUtilities.Tests/EnumUtilsTests.cs b/CSUtilities.Tests/EnumUtilsTests.cs deleted file mode 100644 index 9649519..0000000 --- a/CSUtilities.Tests/EnumUtilsTests.cs +++ /dev/null @@ -1,17 +0,0 @@ -using CSUtilities.Extensions; -using CSUtilities.Tests.Mock; -using Xunit; - -namespace CSUtilities.Tests -{ - public class EnumUtilsTests - { - [Fact] - public void GetStringValueTest() - { - MockEnumWithStringValues value0 = MockEnumWithStringValues.EnumValue_0; - - Assert.Equal("Enum String Value 0", value0.GetStringValue()); - } - } -} diff --git a/CSUtilities.Tests/Extensions/EnumExtensionsTests.cs b/CSUtilities.Tests/Extensions/EnumExtensionsTests.cs new file mode 100644 index 0000000..0010967 --- /dev/null +++ b/CSUtilities.Tests/Extensions/EnumExtensionsTests.cs @@ -0,0 +1,56 @@ +using CSUtilities.Attributes; +using CSUtilities.Extensions; +using System; +using Xunit; + +namespace CSUtilities.Tests.Extensions +{ + public class EnumExtensionsTests + { + private enum MockStringValues + { + [StringValue("Undefined value for enum")] + Undefined = 0, + + [StringValue("Enum String Value 1")] + Value1 = 1, + + [StringValue("Enum String Value 2")] + Value2, + + NoAttribute, + } + + [Fact] + public void GetStringValueTest() + { + Assert.Equal("Undefined value for enum", MockStringValues.Undefined.GetStringValue()); + Assert.Equal("Enum String Value 1", MockStringValues.Value1.GetStringValue()); + Assert.Equal("Enum String Value 2", MockStringValues.Value2.GetStringValue()); + Assert.Null(MockStringValues.NoAttribute.GetStringValue()); + } + + [Fact] + public void ParseTest() + { + Assert.Equal(MockStringValues.Value1, EnumExtensions.Parse("Value1")); + Assert.Equal(MockStringValues.Value1, EnumExtensions.Parse("value1", true)); + Assert.Throws(() => EnumExtensions.Parse("value1", false)); + } + + [Fact] + public void TryParseTest() + { + MockStringValues result = MockStringValues.Undefined; + Assert.True(EnumExtensions.TryParse("Value1", out result)); + Assert.Equal(MockStringValues.Value1, result); + + result = MockStringValues.Undefined; + Assert.True(EnumExtensions.TryParse("value1", out result, true)); + Assert.Equal(MockStringValues.Value1, result); + + Assert.False(EnumExtensions.TryParse("value1", out result, false)); + Assert.Equal(default(MockStringValues), result); + } + } +} diff --git a/CSUtilities.Tests/Mock/MockEnumWithStringValues.cs b/CSUtilities.Tests/Mock/MockEnumWithStringValues.cs deleted file mode 100644 index 11c8d57..0000000 --- a/CSUtilities.Tests/Mock/MockEnumWithStringValues.cs +++ /dev/null @@ -1,18 +0,0 @@ -using CSUtilities.Attributes; - -namespace CSUtilities.Tests.Mock -{ - public enum MockEnumWithStringValues - { - [StringValue("Enum String Value 0")] - EnumValue_0, - [StringValue("Enum String Value 1")] - EnumValue_2, - [StringValue("Enum String Value 2")] - EnumValue_3, - [StringValue("Enum String Value 3")] - EnumValue_4, - [StringValue("Enum String Value 4")] - EnumValue_5, - } -} diff --git a/CSUtilities/Attributes/StringValueAttribute.cs b/CSUtilities/Attributes/StringValueAttribute.cs index 0189067..8be9099 100644 --- a/CSUtilities/Attributes/StringValueAttribute.cs +++ b/CSUtilities/Attributes/StringValueAttribute.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace CSUtilities.Attributes { diff --git a/CSUtilities/Extensions/EnumExtensions.cs b/CSUtilities/Extensions/EnumExtensions.cs index 1f75318..3191b62 100644 --- a/CSUtilities/Extensions/EnumExtensions.cs +++ b/CSUtilities/Extensions/EnumExtensions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; namespace CSUtilities.Extensions @@ -16,7 +17,8 @@ public static IEnumerable GetValues() } [Obsolete("Use Type.GetNames()")] - public static IEnumerable GetValuesNames() + public static IEnumerable GetNames(this T value) + where T : Enum { return Enum.GetValues(typeof(T)).Cast().Select(o => o.ToString()); } @@ -26,12 +28,34 @@ public static T GetValueByName(string name) return Enum.GetValues(typeof(T)).Cast().FirstOrDefault(o => o.ToString() == name); } + public static T Parse(string value, bool ignoreCase = false) + where T : Enum + { + return (T)Enum.Parse(typeof(T), value, ignoreCase); + } + + public static bool TryParse(string value, out T result, bool ignoreCase = false) + where T : Enum + { + try + { + result = Parse(value, ignoreCase); + return true; + } + catch (Exception) + { + result = default(T); + return false; + } + } + /// /// Gets a string value for a particular enum value. /// /// enum value /// String Value associated via a attribute, or null if not found. - public static string GetStringValue(this T value) where T : Enum + public static string GetStringValue(this T value) + where T : Enum { Type type = value.GetType(); From b539c44f858ae65a378a0f75dfdd7264cd2aa3a0 Mon Sep 17 00:00:00 2001 From: DomCR Date: Fri, 5 Jan 2024 16:35:46 +0100 Subject: [PATCH 2/4] parse --- CSUtilities/Extensions/EnumExtensions.cs | 32 +++++++++++++++--------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/CSUtilities/Extensions/EnumExtensions.cs b/CSUtilities/Extensions/EnumExtensions.cs index 3191b62..83b00ba 100644 --- a/CSUtilities/Extensions/EnumExtensions.cs +++ b/CSUtilities/Extensions/EnumExtensions.cs @@ -28,29 +28,37 @@ public static T GetValueByName(string name) return Enum.GetValues(typeof(T)).Cast().FirstOrDefault(o => o.ToString() == name); } + /// + /// Converts the string representation of the name or numeric value of one or more + /// enumerated constants to an equivalent enumerated object + /// + /// + /// + /// + /// public static T Parse(string value, bool ignoreCase = false) where T : Enum { return (T)Enum.Parse(typeof(T), value, ignoreCase); } + /// + /// Converts the string representation of the name or numeric value of one or more + /// enumerated constants to an equivalent enumerated object + /// + /// + /// + /// + /// + /// public static bool TryParse(string value, out T result, bool ignoreCase = false) - where T : Enum + where T : struct { - try - { - result = Parse(value, ignoreCase); - return true; - } - catch (Exception) - { - result = default(T); - return false; - } + return Enum.TryParse(value, ignoreCase, out result); } /// - /// Gets a string value for a particular enum value. + /// Gets a string value for a particular enum value /// /// enum value /// String Value associated via a attribute, or null if not found. From 6e7e429e91f671d4fcc55799e5ad6c8ce6c18b33 Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 22 Jan 2024 17:00:46 +0100 Subject: [PATCH 3/4] enum parse --- CSUtilities.Tests/EnumUtilsTests.cs | 24 ++++++++ CSUtilities/Extensions/EnumExtensions.cs | 60 +++++++++++++++++-- .../Extensions/ReflectionExtensions.cs | 16 ++++- CSUtilities/Extensions/StringExtensions.cs | 9 ++- 4 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 CSUtilities.Tests/EnumUtilsTests.cs diff --git a/CSUtilities.Tests/EnumUtilsTests.cs b/CSUtilities.Tests/EnumUtilsTests.cs new file mode 100644 index 0000000..de79126 --- /dev/null +++ b/CSUtilities.Tests/EnumUtilsTests.cs @@ -0,0 +1,24 @@ +using CSUtilities.Extensions; +using CSUtilities.Tests.Mock; +using System; +using Xunit; + +namespace CSUtilities.Tests +{ + public class EnumUtilsTests + { + [Fact] + public void GetStringValueTest() + { + MockEnumWithStringValues value0 = MockEnumWithStringValues.EnumValue_0; + + Assert.Equal("Enum String Value 0", value0.GetStringValue()); + } + + [Fact] + public void ParseTest() + { + MockEnumWithStringValues e = Enum.Parse(""); + } + } +} diff --git a/CSUtilities/Extensions/EnumExtensions.cs b/CSUtilities/Extensions/EnumExtensions.cs index 83b00ba..bd7fe1c 100644 --- a/CSUtilities/Extensions/EnumExtensions.cs +++ b/CSUtilities/Extensions/EnumExtensions.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; -using System.Text; namespace CSUtilities.Extensions { @@ -36,12 +34,66 @@ public static T GetValueByName(string name) /// /// /// - public static T Parse(string value, bool ignoreCase = false) + public static T Parse(this string value, bool ignoreCase = false) where T : Enum { return (T)Enum.Parse(typeof(T), value, ignoreCase); } + /// + /// Converts the string representation compared with the assigned value of one or more + /// enumerated constants to an equivalent enumerated object + /// + /// + /// + /// + public static T ParseByStringValue(this string value) + where T : Enum + { + foreach (FieldInfo fi in typeof(T).GetFields()) + { + if (!fi.TryGetAttribute(out StringValueAttribute att)) + { + continue; + } + + if (att.Value.Equals(value, StringComparison.InvariantCultureIgnoreCase)) + { + return (T)Enum.Parse(typeof(T), fi.Name); + } + } + + throw new ArgumentNullException(nameof(value)); + } + + /// + /// Converts the string representation compared with the assigned value of one or more + /// enumerated constants to an equivalent enumerated object + /// + /// + /// + /// + public static bool TryParseByStringValue(this string value, out T result) + where T : Enum + { + foreach (FieldInfo fi in typeof(T).GetFields()) + { + if (!fi.TryGetAttribute(out StringValueAttribute att)) + { + continue; + } + + if (att.Value.Equals(value, StringComparison.InvariantCultureIgnoreCase)) + { + result = (T)Enum.Parse(typeof(T), fi.Name); + return true; + } + } + + result = default(T); + return false; + } + /// /// Converts the string representation of the name or numeric value of one or more /// enumerated constants to an equivalent enumerated object @@ -51,7 +103,7 @@ public static T Parse(string value, bool ignoreCase = false) /// /// /// - public static bool TryParse(string value, out T result, bool ignoreCase = false) + public static bool TryParse(this string value, out T result, bool ignoreCase = false) where T : struct { return Enum.TryParse(value, ignoreCase, out result); diff --git a/CSUtilities/Extensions/ReflectionExtensions.cs b/CSUtilities/Extensions/ReflectionExtensions.cs index 2c9c1c2..accb857 100644 --- a/CSUtilities/Extensions/ReflectionExtensions.cs +++ b/CSUtilities/Extensions/ReflectionExtensions.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; namespace CSUtilities.Extensions { @@ -30,5 +28,19 @@ public static bool HasInterface(this Type type) return type.GetInterface(it.FullName) != null; } + + /// + /// Tries to retrive a custom attribute of a specific type to a specified type member. + /// + /// + /// + /// + /// + public static bool TryGetAttribute(this MemberInfo member, out T attribute) + where T : Attribute + { + attribute = member.GetCustomAttribute(); + return attribute != null; + } } } diff --git a/CSUtilities/Extensions/StringExtensions.cs b/CSUtilities/Extensions/StringExtensions.cs index 110b01d..a887c4c 100644 --- a/CSUtilities/Extensions/StringExtensions.cs +++ b/CSUtilities/Extensions/StringExtensions.cs @@ -12,12 +12,17 @@ internal static class StringExtensions { public static bool IsNull(this string str) { - throw new NotImplementedException(); + return str == null; } public static bool IsNullOrEmpty(this string str) { - throw new NotImplementedException(); + return string.IsNullOrEmpty(str); + } + + public static bool IsNullOrWhiteSpace(this string str) + { + return string.IsNullOrWhiteSpace(str); } /// From f82677293b1abd401577810a72fb629f2feb28fd Mon Sep 17 00:00:00 2001 From: DomCR Date: Thu, 21 Mar 2024 17:27:47 +0100 Subject: [PATCH 4/4] test fix --- CSUtilities.Tests/EnumUtilsTests.cs | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 CSUtilities.Tests/EnumUtilsTests.cs diff --git a/CSUtilities.Tests/EnumUtilsTests.cs b/CSUtilities.Tests/EnumUtilsTests.cs deleted file mode 100644 index de79126..0000000 --- a/CSUtilities.Tests/EnumUtilsTests.cs +++ /dev/null @@ -1,24 +0,0 @@ -using CSUtilities.Extensions; -using CSUtilities.Tests.Mock; -using System; -using Xunit; - -namespace CSUtilities.Tests -{ - public class EnumUtilsTests - { - [Fact] - public void GetStringValueTest() - { - MockEnumWithStringValues value0 = MockEnumWithStringValues.EnumValue_0; - - Assert.Equal("Enum String Value 0", value0.GetStringValue()); - } - - [Fact] - public void ParseTest() - { - MockEnumWithStringValues e = Enum.Parse(""); - } - } -}