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 |
38
README.md
38
README.md
@ -1,27 +1,27 @@
|
||||
# ns-ext
|
||||
|
||||
[中](README.zh-CN.md) | **En**
|
||||
The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
|
||||
|
||||
|
||||
| Features | File name |
|
||||
| -------- | ---------------------------------- |
|
||||
| Byte type extension | ByteExtensions.cs |
|
||||
| Character Type Extensions | CharExtensions.cs |
|
||||
| Date Type Extensions | DateTimeExtensions.cs |
|
||||
| Database command type extension | DbCommandExtensions.cs |
|
||||
| Decimal Number Type extension | DecimalExtensions.cs |
|
||||
| Enumable type extension | EnumerableExtensions.cs |
|
||||
| Enumeration type extension | EnumExtensions.cs |
|
||||
| General type extension | GenericExtensions.cs |
|
||||
| Integer type extension | IntExtensions.cs |
|
||||
| Features | File name |
|
||||
|------------------------------------------|------------------------------------|
|
||||
| Byte type extension | ByteExtensions.cs |
|
||||
| Character Type Extensions | CharExtensions.cs |
|
||||
| Date Type Extensions | DateTimeExtensions.cs |
|
||||
| Database command type extension | DbCommandExtensions.cs |
|
||||
| Decimal Number Type extension | DecimalExtensions.cs |
|
||||
| Enumable type extension | EnumerableExtensions.cs |
|
||||
| Enumeration type extension | EnumExtensions.cs |
|
||||
| General type extension | GenericExtensions.cs |
|
||||
| Integer type extension | IntExtensions.cs |
|
||||
| Json Serialization option type extension | JsonSerializerOptionsExtensions.cs |
|
||||
| Log type extension | LoggerExtensions.cs |
|
||||
| Long integer extension | LongExtensions.cs |
|
||||
| Object type extension | ObjectExtensions.cs |
|
||||
| Stream type extension | StreamExtensions.cs |
|
||||
| String type extension | StringExtensions.cs |
|
||||
| Prototype type extension | TypeExtensions.cs |
|
||||
| Resource locator type extension | UriExtensions.cs |
|
||||
| Log type extension | LoggerExtensions.cs |
|
||||
| Long integer extension | LongExtensions.cs |
|
||||
| Object type extension | ObjectExtensions.cs |
|
||||
| Stream type extension | StreamExtensions.cs |
|
||||
| String type extension | StringExtensions.cs |
|
||||
| Prototype type extension | TypeExtensions.cs |
|
||||
| Resource locator type extension | UriExtensions.cs |
|
||||
|
||||
## Quick start
|
||||
|
||||
|
@ -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导致出错
|
||||
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 RegexMobile
|
||||
= 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);
|
||||
}
|
@ -42,17 +42,20 @@ public static class ByteExtensions
|
||||
/// <param name="me">me</param>
|
||||
/// <param name="upperCase">是否大写</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);
|
||||
if (!upperCase) {
|
||||
ret = ret.ToLower(CultureInfo.InvariantCulture);
|
||||
var sb = new StringBuilder();
|
||||
var i = 0;
|
||||
foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) {
|
||||
if (i++ % splitInterval == 0) {
|
||||
sb.Append(splitShar);
|
||||
}
|
||||
|
||||
sb.Append(c);
|
||||
}
|
||||
|
||||
if (splitShar != "-") {
|
||||
ret = ret.Replace("-", splitShar ?? string.Empty);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
@ -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>(true);
|
||||
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>
|
||||
public static string Json(this object me, bool format = false)
|
||||
{
|
||||
return JsonSerializer.Serialize(
|
||||
me, new JsonSerializerOptions { WriteIndented = format, PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
||||
var defaultOptions = default(JsonSerializerOptions).NewJsonSerializerOptions();
|
||||
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();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// url编码
|
||||
/// </summary>
|
||||
|
Reference in New Issue
Block a user