mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-04-20 01:22:51 +08:00
<feat> 从资源文件读取Description
This commit is contained in:
parent
13f8ae51c2
commit
d23092e8fc
@ -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
|
||||
}
|
24
src/Attributes/LocalizationAttribute.cs
Normal file
24
src/Attributes/LocalizationAttribute.cs
Normal 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; }
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
using System.Reflection;
|
||||
using NSExt.Attributes;
|
||||
|
||||
namespace NSExt.Extensions;
|
||||
|
||||
/// <summary>
|
||||
@ -12,9 +15,15 @@ public static class EnumExtensions
|
||||
/// <returns>description属性</returns>
|
||||
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<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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user