mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-18 23:22:51 +08:00
feat: enum、string
This commit is contained in:
parent
d1978d9d9e
commit
f2444b3048
@ -1,6 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -17,7 +17,7 @@ public class TestCase
|
||||
_testOutputHelper = testOutputHelper;
|
||||
}
|
||||
|
||||
public enum MyEnum
|
||||
public enum MyEnum1
|
||||
{
|
||||
[ResourceDescription<TestCase>(nameof(Description))]
|
||||
Online = 1
|
||||
@ -33,7 +33,7 @@ public class TestCase
|
||||
[Fact]
|
||||
public void Case1()
|
||||
{
|
||||
var test = MyEnum.Online.ResDesc<TestCase>();
|
||||
var test = MyEnum1.Online.ResDesc<TestCase>();
|
||||
|
||||
_testOutputHelper.WriteLine(test);
|
||||
Assert.True(test is not null);
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
using NSExt.Attributes;
|
||||
|
||||
@ -13,6 +14,7 @@ public static class EnumExtensions
|
||||
/// </summary>
|
||||
/// <param name="e">枚举对象</param>
|
||||
/// <returns>description属性</returns>
|
||||
[Obsolete(nameof(ResDesc))]
|
||||
public static string Desc(this Enum e)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
@ -495,6 +495,25 @@ public static class StringExtensions
|
||||
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>
|
||||
|
@ -24,7 +24,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<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>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
Loading…
x
Reference in New Issue
Block a user