From d23092e8fca843a98afd0c99a279bc8f53a438f9 Mon Sep 17 00:00:00 2001 From: nsnail Date: Sun, 8 Jan 2023 19:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=20=E4=BB=8E=E8=B5=84=E6=BA=90=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=AF=BB=E5=8F=96Description?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- push2nuget.ps1 | 25 ------------------------- src/Attributes/LocalizationAttribute.cs | 24 ++++++++++++++++++++++++ src/Extensions/EnumExtensions.cs | 17 +++++++++++++---- 3 files changed, 37 insertions(+), 29 deletions(-) delete mode 100644 push2nuget.ps1 create mode 100644 src/Attributes/LocalizationAttribute.cs diff --git a/push2nuget.ps1 b/push2nuget.ps1 deleted file mode 100644 index 24d0798..0000000 --- a/push2nuget.ps1 +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/src/Attributes/LocalizationAttribute.cs b/src/Attributes/LocalizationAttribute.cs new file mode 100644 index 0000000..5658339 --- /dev/null +++ b/src/Attributes/LocalizationAttribute.cs @@ -0,0 +1,24 @@ +namespace NSExt.Attributes; + +/// +/// 指定本地化资源类型 +/// +[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Field)] +public class LocalizationAttribute : Attribute +{ + /// + /// Initializes a new instance of the class. + /// + public LocalizationAttribute(Type resourceClass) + { + ResourceClass = resourceClass; + } + + /// + /// Gets or sets 资源类型 + /// + /// + /// 资源类型 + /// + public Type ResourceClass { get; set; } +} \ No newline at end of file diff --git a/src/Extensions/EnumExtensions.cs b/src/Extensions/EnumExtensions.cs index 80ae7bd..0c75c95 100644 --- a/src/Extensions/EnumExtensions.cs +++ b/src/Extensions/EnumExtensions.cs @@ -1,3 +1,6 @@ +using System.Reflection; +using NSExt.Attributes; + namespace NSExt.Extensions; /// @@ -12,9 +15,15 @@ public static class EnumExtensions /// description属性 public static string Desc(this Enum e) { - 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 t = e.GetType(); + var fi = t.GetField(Enum.GetName(t, e)!); + var descAttr = fi!.GetCustomAttribute(false); + if (descAttr is null) { + return Enum.GetName(t, e); + } + + var str = descAttr.Description; + var locAttr = fi!.GetCustomAttribute(false); + return locAttr is null ? str : locAttr.ResourceClass.GetProperty(str)?.GetValue(default) as string ?? str; } } \ No newline at end of file