CodeLab.Share 1.4.2

dotnet add package CodeLab.Share --version 1.4.2
                    
NuGet\Install-Package CodeLab.Share -Version 1.4.2
                    
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="CodeLab.Share" Version="1.4.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeLab.Share" Version="1.4.2" />
                    
Directory.Packages.props
<PackageReference Include="CodeLab.Share" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CodeLab.Share --version 1.4.2
                    
#r "nuget: CodeLab.Share, 1.4.2"
                    
#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.
#:package CodeLab.Share@1.4.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeLab.Share&version=1.4.2
                    
Install as a Cake Addin
#tool nuget:?package=CodeLab.Share&version=1.4.2
                    
Install as a Cake Tool

CodeLab

nuget nuget codeSize 编程语言

简介

CodeLab.Share 是一个功能丰富的 .NET 工具库,旨在简化日常开发工作。该库封装了许多常用的工具类和扩展方法,大部分以静态类的形式提供,使用方便快捷。

主要功能包括字符串处理、加密解密、反射操作、唯一ID生成、爬虫JSON操作等,适用于各种.NET应用场景。该库持续更新完善中,欢迎使用和贡献。

安装

通过 NuGet 包管理器安装:

dotnet add package CodeLab.Share

或在 Visual Studio 的 NuGet 包管理器中搜索 CodeLab.Share

功能模块

Extensions 扩展方法

  • StringExt: 字符串扩展方法

    • Base64Encrypt/Base64Decrypt: Base64编码和解码
    • Limit/LimitWithEllipsis: 限制字符串长度,可选添加省略号
    • ToSha256/ToSha384/ToSha512: 字符串哈希计算
    // 使用示例
    using CodeLab.Share.Extensions;
    
    // Base64编码解码
    string original = "Hello, World!";
    string encoded = original.Base64Encrypt(); // SGVsbG8sIFdvcmxkIQ==
    string decoded = encoded.Base64Decrypt();  // Hello, World!
    
    // 字符串长度限制
    string longText = "这是一段很长的文本内容,需要进行截断处理";
    string limited = longText.Limit(10);         // "这是一段很长"
    string withEllipsis = longText.LimitWithEllipsis(10); // "这是一段很长..."
    
    // 哈希计算
    string hash = "sensitive data".ToSha256(); // 计算SHA256哈希值
    
  • HttpClientExt: HttpClient扩展方法

    • GetJsonAsync: 快速获取JSON对象的方法
    // 使用示例
    using CodeLab.Share.Extensions;
    using System.Net.Http;
    
    // 快速获取并解析JSON数据
    HttpClient client = new HttpClient();
    var weatherData = await client.GetJsonAsync<WeatherForecast>("https://api.example.com/weather");
    
  • PagedListExt: 分页列表扩展方法

    // 使用示例
    using CodeLab.Share.Extensions;
    using X.PagedList;
    
    // 创建分页数据
    var query = dbContext.Users.AsQueryable();
    var pagedList = await query.ToPagedListAsync(pageNumber, pageSize);
    

Utilities 工具类

  • AesHelper: ECB模式的AES算法加解密

    • EncryptBase64/DecryptBase64: AES加密解密并进行Base64编码
    // 使用示例
    using CodeLab.Share.Utilities;
    
    string key = "1234567890123456"; // 16位密钥
    string plainText = "需要加密的内容";
    
    // 加密
    string encrypted = AesHelper.EncryptBase64(plainText, key);
    
    // 解密
    string decrypted = AesHelper.DecryptBase64(encrypted, key);
    
  • EmailHelper: 邮件发送工具

    // 使用示例
    using CodeLab.Share.Utilities;
    
    // 配置邮件服务器
    var emailConfig = new EmailHelper.EmailConfig {
        SmtpServer = "smtp.example.com",
        Port = 587,
        Username = "your-email@example.com",
        Password = "your-password",
        EnableSsl = true
    };
    
    // 发送邮件
    await EmailHelper.SendEmailAsync(
        emailConfig,
        "recipient@example.com",
        "邮件主题",
        "邮件正文内容",
        isHtml: false
    );
    
  • GuidHelper: 生成各种唯一ID

    // 使用示例
    using CodeLab.Share.Utilities;
    
    // 生成不同格式的唯一ID
    string guid = GuidHelper.NewGuid();       // 标准GUID
    string shortId = GuidHelper.NewShortGuid(); // 短GUID
    string uuid = GuidHelper.NewUuid();        // UUID格式
    
  • HashHelper: 支持使用SHA256/SHA384/SHA512等算法创建字符串摘要

    // 使用示例
    using CodeLab.Share.Utilities;
    
    string input = "需要计算哈希的数据";
    
    // 计算不同算法的哈希值
    string sha256Hash = HashHelper.ComputeSha256Hash(input);
    string sha384Hash = HashHelper.ComputeSha384Hash(input);
    string sha512Hash = HashHelper.ComputeSha512Hash(input);
    
  • UserAgentHelper: 判断UA是否为移动设备访问

    // 使用示例
    using CodeLab.Share.Utilities;
    
    string userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X)...";
    bool isMobile = UserAgentHelper.IsMobile(userAgent); // 返回true
    
  • ViewModelHelper: 使用反射获取System.ComponentModel.DataAnnotations的特性信息

    // 使用示例
    using CodeLab.Share.Utilities;
    using System.ComponentModel.DataAnnotations;
    
    public class UserViewModel {
        [Required]
        [Display(Name = "用户名")]
        public string Username { get; set; }
    }
    
    // 获取属性的Display特性信息
    var displayName = ViewModelHelper.GetDisplayName<UserViewModel>("Username"); // 返回"用户名"
    

Contrib 贡献模块

  • CronExpression: CRON表达式处理工具

    • ValidateExpression: 校验CRON表达式是否有效
    • Next: 获取下一个执行时间
    • IsWithinSchedule: 判断时间是否在调度范围内
    • Timeout: 获取到下一次执行的时间间隔
    // 使用示例
    using CodeLab.Share.Contrib.CronExpression;
    
    // 校验CRON表达式
    CronExpression.ValidateExpression("0 0 12 * * ?"); // 有效表达式不会抛出异常
    
    // 创建CRON表达式对象
    var cron = new CronExpression("0 0 12 * * ?"); // 每天中午12点
    
    // 获取下一次执行时间
    var nextRun = cron.Next(DateTimeOffset.Now);
    
    // 判断当前时间是否在调度范围内
    bool isWithin = cron.IsWithinSchedule(DateTimeOffset.Now);
    
    // 获取到下一次执行的时间间隔
    TimeSpan timeToNext = cron.Timeout(DateTimeOffset.Now);
    
  • StopWords: 敏感词处理工具

    • StopWordsDataManager: 敏感词数据管理
    • StopWordsToolkit: 敏感词工具集
    // 使用示例
    using CodeLab.Share.Contrib.StopWords;
    
    // 初始化敏感词管理器
    var dataManager = new StopWordsDataManager("stopwords.db");
    
    // 使用敏感词工具集
    var toolkit = new StopWordsToolkit(dataManager);
    
    // 过滤文本中的敏感词
    string text = "这是一个包含了很多敏感词的文本";
    string filtered = toolkit.FilterStopWords(text);
    
  • ClrStats: CLR统计工具

    // 使用示例
    using CodeLab.Share.Contrib.ClrStats;
    
    // 获取CPU使用情况
    var cpuUsage = CpuHelper.GetCpuUsage();
    
    // 获取GC信息
    var gcInfo = GcHelper.GetGcInfo();
    

ViewModels 视图模型

  • Response: API响应模型

    • ApiResponse<T>: 泛型API响应类,支持隐式转换
    • ApiResponse: 通用API响应类
    // 使用示例
    using CodeLab.Share.ViewModels.Response;
    
    // 创建成功响应
    var successResponse = ApiResponse.Ok("数据获取成功");
    
    // 创建错误响应
    var errorResponse = ApiResponse.BadRequest("请求参数错误");
    
    // 使用隐式转换
    ApiResponse<User> userResponse = new User { Id = 1, Name = "张三" };
    // 等同于: new ApiResponse<User> { Data = new User { Id = 1, Name = "张三" }, Successful = true, StatusCode = 200 }
    
  • PaginationMetadata: 分页元数据模型

    // 使用示例
    using CodeLab.Share.ViewModels;
    
    // 创建分页元数据
    var metadata = new PaginationMetadata {
        CurrentPage = 1,
        PageSize = 10,
        TotalCount = 100,
        TotalPages = 10
    };
    

build

docs: https://learn.microsoft.com/zh-cn/nuget/create-packages/creating-a-package-dotnet-cli

打包 nuget

dotnet pack -c release

已集成 GitHub Action 工作流,现在不需要手动打包发布了。

使用到这个工具箱的项目

what's new

1.4.2

  • 继续完善文档
  • 添加新的隐式转换操作符到ApiResponse类,允许将类型T直接转换为ApiResponse<T>,简化API响应构建

1.4.1

  • 完善文档
  • CodeLab.Share.Contrib.CronExpression 新增 ValidateExpression 方法
  • New CodeLab.Share.Utilities.AesHelper - ECB模式的AES算法加解密
  • New CodeLab.Share.Utilities.EmailHelper - 发邮件
  • New CodeLab.Share.Utilities.GuidHelper - 生成各种唯一ID
  • New CodeLab.Share.Utilities.HashHelper - 支持使用 SHA256/SHA384/SHA512 等算法创建字符串摘要
  • New CodeLab.Share.Utilities.UserAgentHelper - 判断 UA 是不是手机访问
  • New CodeLab.Share.Utilities.ViewModelHelper - 使用反射获取 System.ComponentModel.DataAnnotations 的特性信息
  • New CodeLab.Share.Extensions.HttpClientExt - 封装了 GetJsonAsync 方法,可以快速获取 JSON 对象
  • New CodeLab.Share.Extensions.StringExt - 各种字符串操作

1.4.0

  • 新增 CodeLab.Share.Contrib.CronExpression 包,用于处理 CRON 表达式
  • 集成 GitHub Action 工作流
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 (1)

Showing the top 1 popular GitHub repositories that depend on CodeLab.Share:

Repository Stars
Deali-Axy/StarBlog
☀StarBlog 是一个基于 .NET Core 开发的现代博客系统,支持 Markdown 文章导入,遵循 RESTful 接口规范。前端基于 Vue + ElementUI 开发,可作为 .NET Core 入门学习项目,同时配套了一系列开发笔记,记录了从零开始构建这个博客系统的全过程,可以帮助学习理解 .Net Core 项目的开发流程。
Version Downloads Last Updated
1.4.2 214 4/30/2025
1.4.1 352 2/5/2024
1.4.0 154 2/5/2024
1.3.7 188 1/16/2024
1.3.6 138 1/16/2024
1.3.3 149 1/16/2024
1.3.2 1,085 6/18/2023
1.3.1 192 6/17/2023
1.3.0 200 6/17/2023
1.2.0 195 6/16/2023
1.1.1 200 6/7/2023
1.1.0 185 6/7/2023
1.0.2 244 4/7/2023
1.0.1 554 12/20/2022
1.0.0 252 12/20/2022