This commit is contained in:
nsnail 2022-09-22 11:44:40 +08:00
parent 847012041a
commit 6fdf879be0
16 changed files with 50 additions and 37 deletions

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class ByteExtensions public static class ByteExtensions
{ {

View File

@ -1,6 +1,6 @@
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
namespace NSExt; namespace NSExt.Extensions;
public static class DateTimeExtensions public static class DateTimeExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class DbCommandExtensions public static class DbCommandExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class DecimalExtensions public static class DecimalExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class EnumExtensions public static class EnumExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class EnumerableExtensions public static class EnumerableExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class GenericExtensions public static class GenericExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class IntExtensions public static class IntExtensions
{ {

View File

@ -1,6 +1,6 @@
// ReSharper disable TemplateIsNotCompileTimeConstantProblem // ReSharper disable TemplateIsNotCompileTimeConstantProblem
namespace NSExt; namespace NSExt.Extensions;
public static class LoggerExtensions public static class LoggerExtensions
{ {

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class LongExtensions public static class LongExtensions
{ {

View File

@ -0,0 +1,33 @@
using Newtonsoft.Json.Serialization;
namespace NSExt.Extensions;
public static class ObjectExtensions
{
/// <summary>
/// 将一个对象序列化成json文本
/// </summary>
/// <param name="me">指定对象</param>
/// <param name="format">是否格式化</param>
/// <returns>json文本</returns>
public static string Json(this object me, bool format = false)
{
return JsonConvert.SerializeObject(me, format ? Formatting.Indented : Formatting.None);
}
/// <summary>
/// 将一个对象序列化成json文本小驼峰属性名
/// </summary>
/// <param name="me">指定对象</param>
/// <param name="format">是否格式化</param>
/// <returns>json文本</returns>
public static string JsonCamelCase(this object me, bool format = false)
{
return JsonConvert.SerializeObject(me,
new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = format ? Formatting.Indented : Formatting.None
});
}
}

View File

@ -2,11 +2,8 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using HMACMD5 = SshNet.Security.Cryptography.HMACMD5;
using HMACSHA1 = SshNet.Security.Cryptography.HMACSHA1;
using MD5 = SshNet.Security.Cryptography.MD5;
namespace NSExt; namespace NSExt.Extensions;
public static class StringExtensions public static class StringExtensions
{ {
@ -280,7 +277,7 @@ public static class StringExtensions
/// <returns>hash摘要的16进制文本形式无连字符小写</returns> /// <returns>hash摘要的16进制文本形式无连字符小写</returns>
public static string Sha1(this string me, Encoding e) public static string Sha1(this string me, Encoding e)
{ {
using var sha1 = HashAlgorithm.Create(); using var sha1 = SHA1.Create();
return BitConverter.ToString(sha1.ComputeHash(e.GetBytes(me))) return BitConverter.ToString(sha1.ComputeHash(e.GetBytes(me)))
.Replace("-", string.Empty) .Replace("-", string.Empty)
.ToLower(CultureInfo.CurrentCulture); .ToLower(CultureInfo.CurrentCulture);
@ -311,7 +308,7 @@ public static class StringExtensions
/// <returns>hash摘要的16进制文本形式无连字符小写</returns> /// <returns>hash摘要的16进制文本形式无连字符小写</returns>
public static string Md5(this string me, Encoding e) public static string Md5(this string me, Encoding e)
{ {
using var md5 = new MD5(); using var md5 = MD5.Create();
return BitConverter.ToString(md5.ComputeHash(e.GetBytes(me))) return BitConverter.ToString(md5.ComputeHash(e.GetBytes(me)))
.Replace("-", string.Empty) .Replace("-", string.Empty)
.ToLower(CultureInfo.CurrentCulture); .ToLower(CultureInfo.CurrentCulture);

View File

@ -1,4 +1,4 @@
namespace NSExt; namespace NSExt.Extensions;
public static class UriExtensions public static class UriExtensions
{ {

View File

@ -9,4 +9,3 @@ global using System.Web;
global using Newtonsoft.Json; global using Newtonsoft.Json;
global using Newtonsoft.Json.Linq; global using Newtonsoft.Json.Linq;
global using System.ComponentModel; global using System.ComponentModel;
global using SshNet.Security.Cryptography;

View File

@ -8,14 +8,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0"/> <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-preview.7.22375.6"/> <PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0-preview.7.22375.6" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2"/> <PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta2" />
<PackageReference Include="SshNet.Security.Cryptography" Version="1.3.0"/>
</ItemGroup> </ItemGroup>

View File

@ -1,15 +0,0 @@
namespace NSExt;
public static class ObjectExtensions
{
/// <summary>
/// 将一个对象序列化成json文本
/// </summary>
/// <param name="me">指定对象</param>
/// <param name="format">是否格式化</param>
/// <returns>json文本</returns>
public static string Json(this object me, bool format = false)
{
return JsonConvert.SerializeObject(me, format ? Formatting.Indented : Formatting.None);
}
}