mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-19 23:42:51 +08:00
feat: enum、string
This commit is contained in:
parent
d1978d9d9e
commit
f2444b3048
@ -1,6 +1,6 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0-preview-20221221-03" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
|
||||||
<PackageReference Include="xunit" Version="2.4.2" />
|
<PackageReference Include="xunit" Version="2.4.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -17,7 +17,7 @@ public class TestCase
|
|||||||
_testOutputHelper = testOutputHelper;
|
_testOutputHelper = testOutputHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum MyEnum
|
public enum MyEnum1
|
||||||
{
|
{
|
||||||
[ResourceDescription<TestCase>(nameof(Description))]
|
[ResourceDescription<TestCase>(nameof(Description))]
|
||||||
Online = 1
|
Online = 1
|
||||||
@ -33,7 +33,7 @@ public class TestCase
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Case1()
|
public void Case1()
|
||||||
{
|
{
|
||||||
var test = MyEnum.Online.ResDesc<TestCase>();
|
var test = MyEnum1.Online.ResDesc<TestCase>();
|
||||||
|
|
||||||
_testOutputHelper.WriteLine(test);
|
_testOutputHelper.WriteLine(test);
|
||||||
Assert.True(test is not null);
|
Assert.True(test is not null);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using NSExt.Attributes;
|
using NSExt.Attributes;
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ public static class EnumExtensions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="e">枚举对象</param>
|
/// <param name="e">枚举对象</param>
|
||||||
/// <returns>description属性</returns>
|
/// <returns>description属性</returns>
|
||||||
|
[Obsolete(nameof(ResDesc))]
|
||||||
public static string Desc(this Enum e)
|
public static string Desc(this Enum e)
|
||||||
{
|
{
|
||||||
var typeOfEnum = e.GetType();
|
var typeOfEnum = e.GetType();
|
||||||
@ -27,6 +29,23 @@ public static class EnumExtensions
|
|||||||
return locAttr is null ? str : locAttr.ResourceClass.GetProperty(str)?.GetValue(default) as string ?? str;
|
return locAttr is null ? str : locAttr.ResourceClass.GetProperty(str)?.GetValue(default) as string ?? str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通过类泛型类型获取特性
|
||||||
|
/// </summary>
|
||||||
|
public static T GetAttributeOfType<T>(this Enum me)
|
||||||
|
where T : Attribute
|
||||||
|
{
|
||||||
|
return me.GetType().GetMember(me.ToString()).First().GetCustomAttributes<T>(false).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取显示特性
|
||||||
|
/// </summary>
|
||||||
|
public static DisplayAttribute GetDisplay(this Enum me)
|
||||||
|
{
|
||||||
|
return me.GetAttributeOfType<DisplayAttribute>();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取枚举的本地化资源描述
|
/// 获取枚举的本地化资源描述
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -495,6 +495,25 @@ public static class StringExtensions
|
|||||||
return $"<pre>{me}</pre>";
|
return $"<pre>{me}</pre>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 首字母小写
|
||||||
|
/// </summary>
|
||||||
|
public static string ToLowerCamelCase(this string me)
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(me)
|
||||||
|
? me
|
||||||
|
: string.Concat( //
|
||||||
|
me[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant(), me.AsSpan(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 首字母大写
|
||||||
|
/// </summary>
|
||||||
|
public static string ToUpperCamelCase(this string me)
|
||||||
|
{
|
||||||
|
return string.IsNullOrWhiteSpace(me) ? me : string.Concat(me[0].ToString().ToUpperInvariant(), me.AsSpan(1));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将连续多个空格替换成一个空格
|
/// 将连续多个空格替换成一个空格
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||||
<PackageReference Include="MinVer" Version="4.3.0-beta.1">
|
<PackageReference Include="MinVer" Version="4.3.0">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user