提供各种数据转换等辅助工具类
License
—
Deps
0
Install Size
—
Vulns
✓ 0
Published
Feb 28, 2026
$ dotnet add package Smart.HelperSmart.Helper is a comprehensive .NET utility library containing various helper classes for common tasks such as data conversion, encryption/checksums, compression, network operations, logging, and more. It supports .NET 8, 9, and 10.
SmartAssemblyLoader for dynamically loading assemblies with dependency resolution.SmartByteBuffer provides efficient, dynamic byte array manipulation with endianness support.SmartCache implements a thread-safe, in-process memory cache with expiration support.SmartConvert offers tools for base conversion, Hex string/Byte array conversion, and more.SmartLog provides a simple, asynchronous file-based logging mechanism.SmartAutoResetEvent provides async auto-reset event with timeout and cancellation support.SmartNetwork includes utilities for IP/MAC address retrieval, Ping, and TCP port checking.Install the package via NuGet:
dotnet add package Smart.Helper
Efficiently handle binary data with automatic resizing and endianness control.
using Smart.Helper;
var buffer = new SmartByteBuffer(isLittleEndian: true);
buffer.WriteInt32(1024);
buffer.WriteString("Hello World");
byte[] data = buffer.Data; // Get the underlying byte array
Thread-safe in-memory caching with automatic expiration.
using Smart.Helper;
// Add item with 5 minutes expiration
SmartCache.AddOrUpdate("user_123", userObject, TimeSpan.FromMinutes(5));
// Retrieve item
var user = SmartCache.Get<UserProfile>("user_123");
Handle various data type conversions.
using Smart.Helper;
// Hex String to Byte Array
byte[] bytes = SmartConvert.HexStringToByteArray("AA BB CC", " ");
// Base Conversion (e.g., Hex to Binary)
string binary = SmartConvert.ConvertBase("FF", 16, 2); // Outputs "11111111"
Network utility operations.
using Smart.Helper;
// Get all valid IPv4 addresses
var ips = SmartNetwork.GetAllValidIPAddresses(IPTypeFlags.IPv4);
// Check if a port is open
bool isOpen = await SmartNetwork.CheckTcpPortAsync("127.0.0.1", 80);
// Async Ping
bool isReachable = await SmartNetwork.PingAsync("www.google.com");
Easy-to-use compression methods.
using Smart.Helper;
byte[] data = Encoding.UTF8.GetBytes("Some data to compress");
// GZip Compression
byte[] compressed = SmartCompress.GZipCompress(data);
// GZip Decompression
byte[] decompressed = SmartCompress.GZipDecompress(compressed);
Simple asynchronous file logging.
using Smart.Helper;
var logger = new SmartLog("MyService");
logger.StartThread(); // Start background writing thread
logger.Info("Service started successfully.");
logger.Error("An error occurred.");
logger.StopThread(); // Flush and stop
Asynchronous auto-reset event with timeout and cancellation support.
using Smart.Helper;
var evt = new SmartAutoResetEvent();
// Wait with timeout
try
{
await evt.WaitAsync(5000); // Wait up to 5 seconds
Console.WriteLine("Signal received!");
}
catch (TimeoutException)
{
Console.WriteLine("Wait timed out!");
}
// Set signal (typically from another thread/task)
evt.Set();
With cancellation token:
using var cts = new CancellationTokenSource();
try
{
await evt.WaitAsync(TimeSpan.FromSeconds(10), cts.Token);
}
catch (TimeoutException)
{
Console.WriteLine("Wait timed out");
}
catch (OperationCanceledException)
{
Console.WriteLine("Wait was cancelled");
}
// Cancel the wait
cts.Cancel();
Smart.Helper 是一个功能丰富的 .NET 工具库,包含用于数据转换、加密/校验、压缩、网络操作、日志记录等常见任务的辅助类。它支持 .NET 8, 9 和 10。
SmartAssemblyLoader 用于动态加载程序集并自动解析依赖项。SmartByteBuffer 提供高效、动态的字节数组操作,支持大小端切换。SmartCache 实现了线程安全的进程内内存缓存,支持过期时间自动清理。SmartConvert 提供进制转换、Hex 字符串与 Byte 数组互转等工具。SmartLog 提供简单、异步的文件日志记录功能。SmartAutoResetEvent 提供支持超时和取消令牌的异步自动重置事件。SmartNetwork 包含 IP/MAC 地址获取、Ping 操作和 TCP 端口检测等工具。通过 NuGet 安装:
dotnet add package Smart.Helper
高效处理二进制数据,支持自动扩容和字节序控制。
using Smart.Helper;
var buffer = new SmartByteBuffer(isLittleEndian: true);
buffer.WriteInt32(1024);
buffer.WriteString("Hello World");
byte[] data = buffer.Data; // 获取底层字节数组
支持自动过期的线程安全内存缓存。
using Smart.Helper;
// 添加缓存项,5分钟后过期
SmartCache.AddOrUpdate("user_123", userObject, TimeSpan.FromMinutes(5));
// 获取缓存项
var user = SmartCache.Get<UserProfile>("user_123");
处理各种数据类型的转换。
using Smart.Helper;
// Hex 字符串转 Byte 数组
byte[] bytes = SmartConvert.HexStringToByteArray("AA BB CC", " ");
// 进制转换 (例如:十六进制转二进制)
string binary = SmartConvert.ConvertBase("FF", 16, 2); // 输出 "11111111"
常用的网络操作工具。
using Smart.Helper;
// 获取所有有效的 IPv4 地址
var ips = SmartNetwork.GetAllValidIPAddresses(IPTypeFlags.IPv4);
// 检查端口是否打开
bool isOpen = await SmartNetwork.CheckTcpPortAsync("127.0.0.1", 80);
// 异步 Ping
bool isReachable = await SmartNetwork.PingAsync("www.baidu.com");
简单易用的压缩方法。
using Smart.Helper;
byte[] data = Encoding.UTF8.GetBytes("Some data to compress");
// GZip 压缩
byte[] compressed = SmartCompress.GZipCompress(data);
// GZip 解压
byte[] decompressed = SmartCompress.GZipDecompress(compressed);
简单的异步文件日志记录器。
using Smart.Helper;
var logger = new SmartLog("MyService");
logger.StartThread(); // 启动后台写入线程
logger.Info("服务启动成功。");
logger.Error("发生了一个错误。");
logger.StopThread(); // 刷新并停止
支持超时和取消令牌的异步自动重置事件。
using Smart.Helper;
var evt = new SmartAutoResetEvent();
// 带超时等待
try
{
await evt.WaitAsync(5000); // 等待最多5秒
Console.WriteLine("收到信号!");
}
catch (TimeoutException)
{
Console.WriteLine("等待超时!");
}
// 设置信号(通常在另一个线程/任务中)
evt.Set();
使用取消令牌:
using var cts = new CancellationTokenSource();
try
{
await evt.WaitAsync(TimeSpan.FromSeconds(10), cts.Token);
}
catch (TimeoutException)
{
Console.WriteLine("等待超时");
}
catch (OperationCanceledException)
{
Console.WriteLine("等待被取消");
}
// 取消等待
cts.Cancel();
Developed by zenglei