Skip to content

Commit

Permalink
add GetEnumFromDisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
lettucebo committed Jun 4, 2022
1 parent 5558195 commit 64486d8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
35 changes: 28 additions & 7 deletions Ci.Extension.ShareCode/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,38 @@ public static bool IsDefined(this Enum value)
}

/// <summary>
/// Check the enum is define or not
/// Get enum value from description
/// </summary>
/// <param name="description">The enum value.</param>
/// <param name="enumType">The enum Type.</param>
/// <returns>Enum description</returns>
public static Enum GetEnumFromDescription(this string description, Type enumType)
/// <typeparam name="T"></typeparam>
/// <param name="description">The enum description.</param>
/// <returns>The enum</returns>
public static T GetEnumFromDescription<T>(this string description) where T : Enum
{
var result = Enum.GetValues(enumType)
.Cast<Enum>()
var result = Enum.GetValues(typeof(T))
.Cast<T>()
.FirstOrDefault(v => v.GetDescription() == description);
if (result == null)
return default;
return result;
}

public static T GetEnumFromDisplayName<T>(this string name) where T : Enum
{
var type = typeof(T);

foreach (var field in type.GetFields())
{
if (Attribute.GetCustomAttribute(field, typeof(DisplayAttribute)) is DisplayAttribute attribute)
{
if (attribute.Name == name)
return (T)field.GetValue(null);
}

if (field.Name == name)
return (T)field.GetValue(null);
}

return default;
}
}
}
30 changes: 25 additions & 5 deletions Ci.Extensions.Test/EnumExtensionUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@ namespace Ci.Extensions.Test
public class EnumExtensionUnitTest
{
[TestMethod]
public void Get_Enum_From_DisplayName()
public void Get_Enum_From_Description()
{
var description = "One";

var actual = description.GetEnumFromDescription(typeof(TestEnum));
var actual = description.GetEnumFromDescription<TestEnum>();

actual.Should().Be(TestEnum.First);
}

[TestMethod]
public void Get_Enum_From_DisplayName_Should_Null()
public void Get_Enum_From_Description_Should_Null()
{
var description = "One1";

var actual = description.GetEnumFromDescription(typeof(TestEnum));
var actual = description.GetEnumFromDescription<TestEnum>();

actual.Should().Be(default);
}

[TestMethod]
public void Get_Enum_From_DisplayName()
{
var description = "Two";

var actual = description.GetEnumFromDisplayName<TestEnum>();

actual.Should().Be(default);
}

[TestMethod]
public void Get_Enum_From_DisplayName_Should_Null()
{
var description = "Two2";

var actual = description.GetEnumFromDisplayName<TestEnum>();

actual.Should().Be(null);
actual.Should().Be(TestEnum.Second);
}
}
}
6 changes: 3 additions & 3 deletions Ci.Extensions.Test/Enums/TestEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Ci.Extensions.Test.Enums
{
public enum TestEnum
{
[Display(Name = "One")]
[Display(Name = "One1")]
[System.ComponentModel.Description("One")]
First = 1,

[Display(Name = "Two")]
[Display(Name = "Two2")]
[System.ComponentModel.Description("Two")]
Second = 2,

[Display(Name = "Three")]
[Display(Name = "Three3")]
[System.ComponentModel.Description("Three")]
Third = 3,
}
Expand Down

0 comments on commit 64486d8

Please sign in to comment.