mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-06-20 01:08:15 +08:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
371d0d11a5 | |||
13f8ae51c2 | |||
485e7a0ead | |||
943d151048 | |||
09352ac5ea |
38
README.md
38
README.md
@ -1,27 +1,27 @@
|
|||||||
# 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 |
|
| Database command type extension | DbCommandExtensions.cs |
|
||||||
| Database command type extension | DbCommandExtensions.cs |
|
| Decimal Number Type extension | DecimalExtensions.cs |
|
||||||
| Decimal Number Type extension | DecimalExtensions.cs |
|
| Enumable type extension | EnumerableExtensions.cs |
|
||||||
| Enumable type extension | EnumerableExtensions.cs |
|
| Enumeration type extension | EnumExtensions.cs |
|
||||||
| Enumeration type extension | EnumExtensions.cs |
|
| General type extension | GenericExtensions.cs |
|
||||||
| General type extension | GenericExtensions.cs |
|
| Integer type extension | IntExtensions.cs |
|
||||||
| Integer type extension | IntExtensions.cs |
|
|
||||||
| Json Serialization option type extension | JsonSerializerOptionsExtensions.cs |
|
| Json Serialization option type extension | JsonSerializerOptionsExtensions.cs |
|
||||||
| Log type extension | LoggerExtensions.cs |
|
| Log type extension | LoggerExtensions.cs |
|
||||||
| Long integer extension | LongExtensions.cs |
|
| Long integer extension | LongExtensions.cs |
|
||||||
| Object type extension | ObjectExtensions.cs |
|
| Object type extension | ObjectExtensions.cs |
|
||||||
| Stream type extension | StreamExtensions.cs |
|
| Stream type extension | StreamExtensions.cs |
|
||||||
| String type extension | StringExtensions.cs |
|
| String type extension | StringExtensions.cs |
|
||||||
| Prototype type extension | TypeExtensions.cs |
|
| Prototype type extension | TypeExtensions.cs |
|
||||||
| Resource locator type extension | UriExtensions.cs |
|
| Resource locator type extension | UriExtensions.cs |
|
||||||
|
|
||||||
## Quick start
|
## Quick start
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.Append(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (splitShar != "-") {
|
return sb.ToString();
|
||||||
ret = ret.Replace("-", splitShar ?? string.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,6 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using NSExt.Attributes;
|
||||||
|
|
||||||
namespace NSExt.Extensions;
|
namespace NSExt.Extensions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -12,9 +15,15 @@ public static class EnumExtensions
|
|||||||
/// <returns>description属性</returns>
|
/// <returns>description属性</returns>
|
||||||
public static string Desc(this Enum e)
|
public static string Desc(this Enum e)
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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