Skip to content

Commit

Permalink
添加枚举描述转枚举的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
j4587698 committed Jan 18, 2023
1 parent 2aaaa45 commit 8242ff3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Jx.Toolbox.Test/EnumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public void GetDescription()
Assert.Equal("测试", TestEnum.test.GetDescription());
Assert.Equal("test1", TestEnum.test1.GetDescription());
}

[Fact]
public void GetEnumFromDescription()
{
Assert.Equal(TestEnum.test, "测试".GetValueFromDescription<TestEnum>());
Assert.Equal(TestEnum.test1, "test1".GetValueFromDescription<TestEnum>());
Assert.Equal(TestEnum.test, typeof(TestEnum).GetValueFromDescription("测试"));
Assert.Throws<ArgumentException>(() => typeof(EnumTest).GetValueFromDescription("测试"));
Assert.Throws<ArgumentException>(() => typeof(TestEnum).GetValueFromDescription("测试1"));
}
}
45 changes: 45 additions & 0 deletions Jx.Toolbox/Extensions/EnumExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,50 @@ public static string GetDescription(this Enum @enum)
var attr = field.GetCustomAttribute(typeof(DescriptionAttribute));
return attr == null ? @enum.ToString() : (attr as DescriptionAttribute)?.Description;
}

/// <summary>
/// 根据枚举的描述获取枚举
/// </summary>
/// <param name="description">描述</param>
/// <typeparam name="T">枚举类型</typeparam>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static T GetValueFromDescription<T>(this string description) where T : Enum
{
var type = typeof(T);
return (T)GetValueFromDescription(type, description);
}

/// <summary>
/// 根据枚举的描述获取枚举
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="description">描述</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static object GetValueFromDescription(this Type enumType, string description)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("类型不是枚举类型");
}
foreach(var field in enumType.GetFields())
{
if (Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) is DescriptionAttribute attribute)
{
if (attribute.Description == description)
return field.GetValue(null);
}
else
{
if (field.Name == description)
return field.GetValue(null);
}
}

throw new ArgumentException("未找到.", nameof(description));
// Or return default(T);
}
}
}
2 changes: 1 addition & 1 deletion Jx.Toolbox/Jx.Toolbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/j4587698/Jx.Toolbox</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>tool</PackageTags>
<PackageVersion>0.3.1</PackageVersion>
<PackageVersion>0.3.2</PackageVersion>
</PropertyGroup>

</Project>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ EnumExtension
"字符串".ToEnum(转换失败返回枚举类型, 是否区分大小写); // 字符串转换为枚举类型,如果转换失败,则返回默认类型
枚举类型.ToEnum(枚举字符串, 是否区分大小写); // 字符串转换为枚举类型
枚举值.GetDescription(); // 获取枚举的Description,如果没有,则返回内容
枚举类型.ToEnum(枚举描述); // 根据枚举的描述获取枚举
"枚举描述字符串".ToEnum<枚举类型>(); // 根据枚举的描述获取枚举
```

StringArrayExtension
Expand Down

0 comments on commit 8242ff3

Please sign in to comment.