DotNetCommon 8.0.0

dotnet add package DotNetCommon --version 8.0.0                
NuGet\Install-Package DotNetCommon -Version 8.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DotNetCommon" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetCommon --version 8.0.0                
#r "nuget: DotNetCommon, 8.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DotNetCommon as a Cake Addin
#addin nuget:?package=DotNetCommon&version=8.0.0

// Install DotNetCommon as a Cake Tool
#tool nuget:?package=DotNetCommon&version=8.0.0                

DotNetCommon

介绍

搜集.neter开发常用的功能,运行环境: net8.0;

QQ交流群:864844127

主要包版本:

版本 下载 License
DotNetCommon DotNetCommon stats GitHub license
DotNetCommon.Core DotNetCommon.Core stats GitHub license
DotNetCommon.PinYin DotNetCommon.PinYin stats GitHub license
DotNetCommon.Compress DotNetCommon.Compress stats GitHub license

各个包关系如下图:

image-20221101092958287

说明:

  1. DotNetCommon包 具有所有功能,引用了其他的各个功能包;

  2. DotNeCommon.Core 是核心包,有大量常用的方法,不依赖任何包;

  3. DotNetCommon.PinYin 是汉字转拼音包,从https://github.com/toolgood/ToolGood.Words.Pinyin搬运;

  4. DotNetCommon.Window.Registry 是操作window注册表,里面就一个RegistryHelper.cs

  5. DotNetCommon.VerfiyCode 是生成验证码的,从 csdn博文:SkiaSharp 生成验证码 .net6 生成验证码 处搬运;

    .net6之后,System.Drawing.Common包已被标记为Window平台下专用,所以此功能依赖SkiaSharp,这个包是跨平台的,微软在维护。

  6. DotNetCommon.Compress是压缩/解压缩的,里面就一个 CompressHelper.cs

功能列表

  1. 通用数据模型;
  2. 通用树状结构&平铺数据的访问;
  3. 通用代码块上下文模型
  4. 通用数据类型转换之Object.To方法;
  5. 通用Dto间转换之Object.Mapper扩展;
  6. 递归篡改对象的属性值之Modify扩展;
  7. 将Dto属性投影到Entity之ModifyByDto扩展;
  8. 校验框架;
  9. 防抖函数生成器;
  10. 编码和加解密;
  11. 序列化;
  12. 汉字转拼音;
  13. 压缩&解压缩;
  14. 注册表;
  15. 验证码生成;
  16. 随机数;
  17. 对象池;
  18. 断言工具值AssertUtil;
  19. 快速反射工具(Accessor);
  20. 图片格式尺寸检测工具之ImageDetector;
  21. 通用资源释放工具;
  22. 网络帮助类;
  23. 单位转换器(B/KB/MS/GB);
  24. 金额大小写转换;
  25. 枚举类型扩展方法;
  26. 常用扩展方法;
  27. 中文乱码检测(GBK or UTF-8);
  28. 读取Properties文件;
  29. 通用日志Logger;
  30. 代理流(AgentStream);
  31. 异步锁(AsyncLocker);
  32. 表达式帮助类(ExpressionHelper);
  33. 对象(poco)深度克隆;
  34. 通用国际化简易框架;

更多功能介绍

查看:https://gitee.com/jackletter/DotNetCommon/tree/master/docs

快速开始

1.安装包

dotnet add package DotNetCommon

2. 引入命名空间

using DotNetCommon;
using DotNetCommon.Extensions;

3. 功能示例

3.1 数据模型
public Result<Person> GetUserById(int id)
{
    if (id < 0) return Result.NotOk("id必须大于0!");
    //...
    return Result.Ok(new Person());
}
3.2 加解密
public void EncryptTest()
{
    var sensitiveData = "机密数据";
    var key = "12345678"; 
    //加密
    var res = DESHelper.Encrypt(sensitiveData, key);
    //解密
    var res2= DESHelper.Decrypt(res, key);
}
3.3 对象(poco)深度克隆
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Person> Students { get; set; }
}
public void Test()
{
    //没有循环引用的深度克隆
    var list = new List<Person>()
    {
        new Person { Id = 1, Name = "lisa"},
        new Person 
        { 
            Id = 2, 
            Name = "tom", 
            Students = new List<Person> 
                       { 
                            new Person{ Id = 3, Name = "jack" } 
                       };
        },
    };
    list.DeepClone(false);

    //存在循环引用的深度克隆
    var list2 = new List<Person>()
    {
        new Person { Id = 1, Name = "lisa"},
        new Person { Id = 2, Name = "tom"},
    };
    list2[0].Students = new List<Person>{ list2[0] };
    list2.DeepClone(false);
}
3.4 序列化
/// <summary>
/// 指定常用的设置,序列化为json字符串(使用 System.Text.Json)
/// </summary>
/// <param name="obj"></param>
/// <param name="dateTimeFormatString">日期时间格式(DateTime)</param>
/// <param name="dateTimeOffsetFormatString">日期时间格式(DateTimeOffset)</param>
/// <param name="dateOnlyFormatString">日期时间格式(DateOnly)</param>
/// <param name="timeOnlyFormatString">日期时间格式(TimeOnly)</param>
/// <param name="ignoreNull">是否忽略null值的属性</param>
/// <param name="enum2String">是否将枚举转换为字符串</param>
/// <param name="lowerCamelCase">属性名称的首字母是否小写</param>
/// <param name="lowerCamelCaseDictionaryKey">字典key首字母是否小写</param>
/// <param name="isIntend">是否格式缩进</param>
/// <param name="number2String">是否将数字转为字符串</param>
/// <param name="ignoreJsonPropertyName">是否忽略注解: [JsonPropertyName("xxxx")]</param>
/// <param name="otherSettings">其他的设置</param>
/// <remarks>注意: 虽然提供了 <c>lowerCamelCase</c> 和 <c>lowerCamelCaseDictionaryKey</c>, 但没有办法将 <c>JsonObject</c> 中的key首字母小写</remarks>
public static string ToJsonFast(this object obj,
    string dateTimeFormatString = null,
    string dateTimeOffsetFormatString = null,
    string dateOnlyFormatString = null,
    string timeOnlyFormatString = null,
    bool ignoreNull = false,
    bool enum2String = false,
    bool lowerCamelCase = false,
    bool lowerCamelCaseDictionaryKey = false,
    bool isIntend = false,
    bool number2String = false,
    bool ignoreJsonPropertyName = false,
    Action<JsonSerializerOptions> otherSettings = null)

/// <summary>
/// 对已有的 <c>JsonSerializerOptions</c> 进行配置,以增强兼容性,可用在 web开发中,如:
/// <code>
/// services.AddControllers().AddJsonOptions(options =>JsonHelper.Configure(options.JsonSerializerOptions, dateTimeFormatString: "yyyy-MM-dd", lowerCamelCase: true));
/// </code>
/// </summary>
public static void Configure(JsonSerializerOptions options,
    string dateTimeFormatString = null,
    string dateTimeOffsetFormatString = null,
    string dateOnlyFormatString = null,
    string timeOnlyFormatString = null,
    bool enum2String = false,
    bool ignoreNull = false,
    bool lowerCamelCase = false,
    bool lowerCamelCaseDictionaryKey = false,
    bool isIntend = false,
    bool number2String = false,
    bool ignoreJsonPropertyName = false,
    Action<JsonSerializerOptions> otherSettings = null)
3.5 压缩&解压缩
//压缩多个文件
CompressHelper.CompressZip("c:/test.zip", new Dictionary<string, string>
{
    {"中文B222.txt","c:/testfolder/中文B.txt" },
    {"testsubfolder/suba222.txt","c:/testfolder/testsubfolder/testsubfolder-suba.txt" },
    {"testfolder-a.txt","c:/testfolder/testfolder-a.txt" }
});
//压缩目录
CompressHelper.CompressZipFolder("c:/test.zip", "c:/testfolder");

//解压缩 支持 zip/7z/rar/gz/bz2/tar.gz/tar.bz2
CompressHelper.UnCompress("c:/testfolder.7z"), "c:/testfolder");
CompressHelper.UnCompress("c:/testfolder.tar.gz"), "c:/testfolder2");
3.6 类似AutoMapper的转换
public class Cat
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime Birth { get; set; }
}

public class CatDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age
    {
        get
        {
            return DateTime.Now.Year - Birth.Year;
        }
    }
    public DateTime Birth { get; set; }
}

//转换示例
var cat = new Cat()
{
    Id = 1,
    Name = "小明",
    Birth = DateTime.Parse("1989-01-02"),
    Age = 20
};
var dto = cat.Mapper<CatDto>();
dto.ShouldNotBeNull();
dto.Id.ShouldBe(1);
dto.Name.ShouldBe("小明");
dto.Age.ShouldNotBe(20);
3.7 类FluentValidation校验组件
//Service层方法,添加实体
public Result<bool> AddStudent(Teacher teacher)
{
    var res = VValid.ValidResult(teacher);
    if (!res.Success) return res;
    //...新增操作
    return Result.Ok(true);
}

public class Teacher
{
    [VMustGreaterThanZero]
    public int Id { get; set; }

    [VMustIdCard("身份证号不正确")]
    public string IdCard { get; set; }

    [DisplayName("姓名")]
    [VNotNullOrWhiteSpace]
    public string Name { get; set; }

    [VMustGreaterThanZeroIfNotNull]
    [VMustLessThanIfNotNull(200)]
    public int? Age { get; set; }

    [VEach(typeof(VLengthInRangeAttribute), [2, 4])]
    [VNotNullOrEmpty]
    public List<string> StudentNames { get; set; }

    [VMustMatchRegexIfNotNull("^[^0][0-9]{10}$", "qq号不正确")]
    public string QQ { get; set;}

    [VNotNullOrEmpty]
    [VEach(typeof(VNotNull))]
    public List<Book> Books { get; set; }
}
public class Book
{
    [VMustGuid(format:"N", ignoreCase:false, errorMessage:"书籍UUID不正确, 正确格式: [a-f|0-9]{32,32}")]
    public string UUID { get; set; }

    [VNotNullOrWhiteSpace]
    public string Name { get; set; }
}
3.8 注册表
public void Test2()
{
    var path = @"HKEY_CURRENT_USER\TestApplication\Res";
    //判断是否存在
    RegistryHelper.Exists(path);
    //删除项
    RegistryHelper.DeletePath(path);
    //设置值
    RegistryHelper.SetString(path, "name", "小明");
    //读取值
    var name = RegistryHelper.GetString(path, "name");
}

更多介绍,参考:https://gitee.com/jackletter/DotNetCommon/tree/master/docs

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 68 2/26/2025
4.4.0 291 9/18/2023
4.1.0 143 8/15/2023
4.0.1 171 6/5/2023
4.0.0 419 2/2/2023
3.0.0 421 11/1/2022
2.10.0 380 10/30/2022
2.9.3 410 10/20/2022
2.9.2 464 9/22/2022
2.9.1 406 9/22/2022
2.9.0 401 9/21/2022
2.8.5 420 9/13/2022
2.8.4 407 8/24/2022
2.8.3 426 7/29/2022
2.8.2 435 6/20/2022
2.7.2 458 4/28/2022
2.7.1 405 4/26/2022
2.7.0 415 4/18/2022
2.6.0 442 3/30/2022
2.5.0 467 1/24/2022
2.4.1 269 1/9/2022
2.3.0 313 12/6/2021
2.2.0 330 11/14/2021
2.1.4 340 10/21/2021
2.1.3 346 9/26/2021
2.1.2 407 9/26/2021
2.1.1 338 9/21/2021
2.1.0 446 9/11/2021
2.0.1 408 9/5/2021
2.0.0 384 8/27/2021
1.1.2 383 8/20/2021
1.1.1 352 8/13/2021
1.1.0 1,077 6/19/2021
1.0.0 404 2/3/2021