CSharp.Net.Util
7.4.0
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package CSharp.Net.Util --version 7.4.0
NuGet\Install-Package CSharp.Net.Util -Version 7.4.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="CSharp.Net.Util" Version="7.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharp.Net.Util" Version="7.4.0" />
<PackageReference Include="CSharp.Net.Util" />
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 CSharp.Net.Util --version 7.4.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CSharp.Net.Util, 7.4.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.
#:package CSharp.Net.Util@7.4.0
#: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=CSharp.Net.Util&version=7.4.0
#tool nuget:?package=CSharp.Net.Util&version=7.4.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CSharp.Net.Util
一个面向 .NET 项目的通用工具类库,提供日常开发中常用的扩展方法、辅助类和基础组件,减少重复代码,方便快速构建应用程序。
主要功能
- 日志:支持 Debug、Info、Warn、Error 等日志级别,可输出到文件。
- 加密与安全:提供 AES、DES、RSA、MD5、Hash、HMAC 等常用加密和摘要算法工具。
- JSON 处理:提供 JSON 序列化、反序列化、命名策略及常用类型转换器。
- HTTP 与网络通信:提供 HttpClient 请求辅助、请求扩展、Socket、TCP 和 WebSocket 基础组件。
- 文件与压缩:提供文件操作、ZIP、GZip、Deflate 及其他压缩处理工具。
- 对象与数据转换:提供实体转换、反射、对象属性、DataReader、DataRow 和集合转换扩展。
- 字符串与日期时间:提供字符串处理、比较、正则、日期时间格式化及时间计算辅助方法。
- 集合与 LINQ 扩展:提供 IEnumerable、IQueryable、Dictionary、数组、排序及表达式相关扩展。
- 分页与查询:提供分页参数、分页结果和常用查询辅助模型。
- 验证:提供必填、长度、数字、邮箱、手机号、身份证号等数据验证特性。
- 限流与重试:提供令牌桶、限流器、重试及高精度延时等基础组件。
- 标识与并发工具:提供雪花算法 ID、单例、延迟初始化字典、锁、共享内存和线程辅助工具。
- 业务辅助:提供银行卡信息、身份证、IP、钉钉机器人、MSMQ 等常用辅助功能。
- 诊断与系统工具:提供进程监控、应用域、内存、堆栈及部分底层系统操作辅助。
安装
dotnet add package CSharp.Net.Util
主要功能示例
以下示例展示常用入口,具体参数和返回值请以当前版本 API 为准。
日志
LogHelper.Debug("应用程序启动成功");
LogHelper.Info("用户登录成功");
LogHelper.Error("处理请求失败");
加密与摘要
using CSharp.Net.Util.Cryptography;
var md5 = MD5.Md5Encrypt("hello");
var base64 = MD5.MD5Base64String("hello");
JSON
var json = JsonHelper.Serialize(new { Name = "Tom", Age = 18 });
var user = JsonHelper.Deserialize<User>(json);
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
日期时间
var date = DateTimeHelper.GetNowDate();
var time = DateTimeHelper.GeNowTime();
var dateTime = DateTimeHelper.GetNowDateTime();
GZip 压缩
using CSharp.Net.Util.Compression;
var compressed = GZipHelper.CompressString("需要压缩的文本");
var original = GZipHelper.DecompressString(compressed);
分页
var page = new PageArgument(pageIndex: 2, size: 20);
var offset = page.ItemIndex;
PageIndex 从 1 开始,PageSize 未设置或小于等于 0 时默认为 10。
数据验证
using CSharp.Net.Util.Validate;
var result = Validate.ValidataData(model);
if (!result.Flag)
{
Console.WriteLine(result.ErrorMessage);
}
// 验证失败时抛出 ArgumentException
Validate.Vaild(model);
文件
using CSharp.Net.Util;
var fileExists = FileHelper.IsFileExists("data.txt");
var directory = FileHelper.GetRealPath("data", "logs");
HTTP 请求
HttpClientUtil 支持 GET、JSON POST、表单提交、文件上传、自定义请求头、超时和失败重试等常用场景。
// GET
var content = await HttpClientUtil.GetAsync(
"https://example.com/api/users",
timeoutSeconds: 10,
retryNum: 2);
// 带查询参数和请求头的 GET
var parameters = new Dictionary<string, string>
{
["keyword"] = "test",
["page"] = "1"
};
var headers = new Dictionary<string, string>
{
["Authorization"] = "Bearer token"
};
var result = await HttpClientUtil.GetAsync(
"https://example.com/api/search",
parameters,
headers,
timeOutSecond: 10);
// JSON POST
var response = await HttpClientUtil.PostAsync(
"https://example.com/api/users",
new { Name = "Tom", Age = 18 },
headers,
timeOutSecond: 10);
默认请求失败时会抛出异常;如需失败时返回空字符串,可设置 throwEx: false:
var content = await HttpClientUtil.GetAsync(
"https://example.com/api/status",
timeoutSeconds: 5,
throwEx: false);
许可证
本项目采用 MIT 许可证。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 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 is compatible. 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 is compatible. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Text.Json (>= 10.0.5)
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on CSharp.Net.Util:
| Package | Downloads |
|---|---|
|
CSharp.Net.Cache
.NET Cache:Memorycache and Redis |
|
|
CSharp.Net.Mvc
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.