mirror of
				https://github.com/nsnail/ns-ext.git
				synced 2025-11-04 15:30:49 +08:00 
			
		
		
		
	<feat> 从资源文件读取Description
This commit is contained in:
		@@ -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;
 | 
					namespace NSExt.Extensions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// <summary>
 | 
					/// <summary>
 | 
				
			||||||
@@ -14,7 +17,13 @@ public static class EnumExtensions
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        var t        = e.GetType();
 | 
					        var t        = e.GetType();
 | 
				
			||||||
        var fi       = t.GetField(Enum.GetName(t, e)!);
 | 
					        var fi       = t.GetField(Enum.GetName(t, e)!);
 | 
				
			||||||
        var attrs = (DescriptionAttribute[])fi!.GetCustomAttributes(typeof(DescriptionAttribute), false);
 | 
					        var descAttr = fi!.GetCustomAttribute<DescriptionAttribute>(false);
 | 
				
			||||||
        return (attrs.Length != 0 ? attrs[0].Description : Enum.GetName(t, e)) ?? string.Empty;
 | 
					        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;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user