Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensions refactor #7

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions CSUtilities.Tests/EnumUtilsTests.cs

This file was deleted.

56 changes: 56 additions & 0 deletions CSUtilities.Tests/Extensions/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<MockStringValues>("Value1"));
Assert.Equal(MockStringValues.Value1, EnumExtensions.Parse<MockStringValues>("value1", true));
Assert.Throws<ArgumentException>(() => EnumExtensions.Parse<MockStringValues>("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<MockStringValues>("value1", out result, true));
Assert.Equal(MockStringValues.Value1, result);

Assert.False(EnumExtensions.TryParse<MockStringValues>("value1", out result, false));
Assert.Equal(default(MockStringValues), result);
}
}
}
18 changes: 0 additions & 18 deletions CSUtilities.Tests/Mock/MockEnumWithStringValues.cs

This file was deleted.

2 changes: 0 additions & 2 deletions CSUtilities/Attributes/StringValueAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CSUtilities.Attributes
{
Expand Down
91 changes: 88 additions & 3 deletions CSUtilities/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CSUtilities.Extensions
{
Expand All @@ -16,7 +15,8 @@ public static IEnumerable<T> GetValues<T>()
}

[Obsolete("Use Type.GetNames()")]
public static IEnumerable<string> GetValuesNames<T>()
public static IEnumerable<string> GetNames<T>(this T value)
where T : Enum
{
return Enum.GetValues(typeof(T)).Cast<T>().Select(o => o.ToString());
}
Expand Down Expand Up @@ -46,10 +46,95 @@ public static T RemoveFlag<T>(this T value, T flag)

/// <summary>
/// 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
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static T Parse<T>(this string value, bool ignoreCase = false)
where T : Enum
{
return (T)Enum.Parse(typeof(T), value, ignoreCase);
}

/// <summary>
/// Converts the string representation compared with the assigned <see cref="StringValueAttribute"/> value of one or more
/// enumerated constants to an equivalent enumerated object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T ParseByStringValue<T>(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));
}

/// <summary>
/// Converts the string representation compared with the assigned <see cref="StringValueAttribute"/> value of one or more
/// enumerated constants to an equivalent enumerated object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static bool TryParseByStringValue<T>(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;
}

/// <summary>
/// Converts the string representation of the name or numeric value of one or more
/// enumerated constants to an equivalent enumerated object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="result"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static bool TryParse<T>(this string value, out T result, bool ignoreCase = false)
where T : struct
{
return Enum.TryParse(value, ignoreCase, out result);
}

/// <summary>
/// Gets a string value for a particular enum value
/// </summary>
/// <param name="value">enum value</param>
/// <returns>String Value associated via a <see cref="StringValueAttribute"/> attribute, or null if not found.</returns>
public static string GetStringValue<T>(this T value) where T : Enum
public static string GetStringValue<T>(this T value)
where T : Enum
{
Type type = value.GetType();

Expand Down
16 changes: 14 additions & 2 deletions CSUtilities/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CSUtilities.Extensions
{
Expand Down Expand Up @@ -30,5 +28,19 @@ public static bool HasInterface<T>(this Type type)

return type.GetInterface(it.FullName) != null;
}

/// <summary>
/// Tries to retrive a custom attribute of a specific type to a specified type member.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="member"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public static bool TryGetAttribute<T>(this MemberInfo member, out T attribute)
where T : Attribute
{
attribute = member.GetCustomAttribute<T>();
return attribute != null;
}
}
}
9 changes: 7 additions & 2 deletions CSUtilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
Loading