<feat> 从资源文件读取Description

This commit is contained in:
nsnail 2023-01-08 19:58:16 +08:00
parent 13f8ae51c2
commit d23092e8fc
3 changed files with 37 additions and 29 deletions

View File

@ -1,25 +0,0 @@
Param(
# Nuget APIKey
[string] $apikey
)
if ($apikey -eq $null -or $apikey -eq "")
{
Write-Error "require apiKey";
return;
}
rm -r ./build/nupkgs
dotnet build -c Release
$files = Get-ChildItem -Path ./build/nupkgs/ -Filter *.nupkg
foreach ($file in $files)
{
dotnet nuget push $file.fullName --skip-duplicate --api-key $apikey --source https://api.nuget.org/v3/index.json
nuget add $file.fullName -source d:\nuget-pkg
}
$files = Get-ChildItem -Path ./build/nupkgs/ -Filter *.snupkg
foreach ($file in $files)
{
dotnet nuget push $file.fullName --skip-duplicate --api-key $apikey --source https://api.nuget.org/v3/index.json
nuget add $file.fullName -source d:\nuget-pkg
}

View File

@ -0,0 +1,24 @@
namespace NSExt.Attributes;
/// <summary>
/// 指定本地化资源类型
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Field)]
public class LocalizationAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="LocalizationAttribute" /> class.
/// </summary>
public LocalizationAttribute(Type resourceClass)
{
ResourceClass = resourceClass;
}
/// <summary>
/// Gets or sets 资源类型
/// </summary>
/// <value>
/// 资源类型
/// </value>
public Type ResourceClass { get; set; }
}

View File

@ -1,3 +1,6 @@
using System.Reflection;
using NSExt.Attributes;
namespace NSExt.Extensions;
/// <summary>
@ -14,7 +17,13 @@ public static class EnumExtensions
{
var t = e.GetType();
var fi = t.GetField(Enum.GetName(t, e)!);
var attrs = (DescriptionAttribute[])fi!.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attrs.Length != 0 ? attrs[0].Description : Enum.GetName(t, e)) ?? string.Empty;
var descAttr = fi!.GetCustomAttribute<DescriptionAttribute>(false);
if (descAttr is null) {
return Enum.GetName(t, e);
}
var str = descAttr.Description;
var locAttr = fi!.GetCustomAttribute<LocalizationAttribute>(false);
return locAttr is null ? str : locAttr.ResourceClass.GetProperty(str)?.GetValue(default) as string ?? str;
}
}