mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-06-20 06:08:15 +08:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
05ca80acda | |||
cc761e4939 | |||
d23092e8fc | |||
13f8ae51c2 | |||
485e7a0ead | |||
943d151048 | |||
09352ac5ea |
@ -1,10 +1,10 @@
|
|||||||
# ns-ext
|
# ns-ext
|
||||||
|
|
||||||
[中](README.zh-CN.md) | **En**
|
[中](README.zh-CN.md) | **En**
|
||||||
The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
|
The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
|
||||||
|
|
||||||
|
|
||||||
| Features | File name |
|
| Features | File name |
|
||||||
| -------- | ---------------------------------- |
|
|------------------------------------------|------------------------------------|
|
||||||
| Byte type extension | ByteExtensions.cs |
|
| Byte type extension | ByteExtensions.cs |
|
||||||
| Character Type Extensions | CharExtensions.cs |
|
| Character Type Extensions | CharExtensions.cs |
|
||||||
| Date Type Extensions | DateTimeExtensions.cs |
|
| Date Type Extensions | DateTimeExtensions.cs |
|
||||||
|
@ -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; }
|
||||||
|
}
|
@ -4,10 +4,16 @@ namespace NSExt.Constant;
|
|||||||
// 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错
|
// 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错
|
||||||
internal static class Regexes
|
internal static class Regexes
|
||||||
{
|
{
|
||||||
|
public static readonly Regex RegexBacksLantUnicode
|
||||||
|
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
public static readonly Regex RegexMobile
|
public static readonly Regex RegexMobile
|
||||||
= new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
= new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
public static readonly Regex RegexPercentUnicode
|
||||||
|
= new("\\\\u([a-fA-F0-9]{4})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
@ -42,17 +42,20 @@ public static class ByteExtensions
|
|||||||
/// <param name="me">me</param>
|
/// <param name="me">me</param>
|
||||||
/// <param name="upperCase">是否大写</param>
|
/// <param name="upperCase">是否大写</param>
|
||||||
/// <param name="splitShar">字节间分隔符</param>
|
/// <param name="splitShar">字节间分隔符</param>
|
||||||
public static string String(this byte[] me, bool upperCase = true, string splitShar = null)
|
/// <param name="splitInterval">分隔符跳跃字节数</param>
|
||||||
|
public static string String(this IEnumerable<byte> me, bool upperCase = true, string splitShar = ""
|
||||||
|
, int splitInterval = 1)
|
||||||
{
|
{
|
||||||
var ret = BitConverter.ToString(me);
|
var sb = new StringBuilder();
|
||||||
if (!upperCase) {
|
var i = 0;
|
||||||
ret = ret.ToLower(CultureInfo.InvariantCulture);
|
foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) {
|
||||||
|
if (i++ % splitInterval == 0) {
|
||||||
|
sb.Append(splitShar);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (splitShar != "-") {
|
sb.Append(c);
|
||||||
ret = ret.Replace("-", splitShar ?? string.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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>(true);
|
||||||
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>(true);
|
||||||
|
return locAttr is null ? str : locAttr.ResourceClass.GetProperty(str)?.GetValue(default) as string ?? str;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,19 @@ public static class ObjectExtensions
|
|||||||
/// <returns>json文本</returns>
|
/// <returns>json文本</returns>
|
||||||
public static string Json(this object me, bool format = false)
|
public static string Json(this object me, bool format = false)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Serialize(
|
var defaultOptions = default(JsonSerializerOptions).NewJsonSerializerOptions();
|
||||||
me, new JsonSerializerOptions { WriteIndented = format, PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
defaultOptions.WriteIndented = format;
|
||||||
|
return Json(me, defaultOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将一个对象序列化成json文本
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="me">指定对象</param>
|
||||||
|
/// <param name="options">序列化选项</param>
|
||||||
|
/// <returns>json文本</returns>
|
||||||
|
public static string Json(this object me, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return JsonSerializer.Serialize(me, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -508,6 +508,16 @@ public static class StringExtensions
|
|||||||
return ret == me ? ret : ret.TrimSpaces();
|
return ret == me ? ret : ret.TrimSpaces();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将\ux0000 、 %u0000 、 &#x0000; 编码转换成可读字符串
|
||||||
|
/// </summary>
|
||||||
|
public static string UnicodeDe(this string me)
|
||||||
|
{
|
||||||
|
const string replacement = "&#x$1;";
|
||||||
|
return me.Contains(@"\u") ? Regexes.RegexBacksLantUnicode.Replace(me, replacement).HtmlDe() :
|
||||||
|
me.Contains(@"%u") ? Regexes.RegexPercentUnicode.Replace(me, replacement).HtmlDe() : me.HtmlDe();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// url编码
|
/// url编码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Reference in New Issue
Block a user