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 44d063c..76b7825 100644 --- a/CSUtilities/Extensions/EnumExtensions.cs +++ b/CSUtilities/Extensions/EnumExtensions.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; namespace CSUtilities.Extensions { @@ -16,7 +15,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()); } @@ -46,10 +46,95 @@ public static T RemoveFlag(this T value, T flag) /// /// Gets a string value for a particular enum value. + /// 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(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 + /// + /// + /// + /// + /// + /// + public static bool TryParse(this string value, out T result, bool ignoreCase = false) + where T : struct + { + return Enum.TryParse(value, ignoreCase, out result); + } + + /// + /// 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(); 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); } ///