Smart.Helper 3.5.4

dotnet add package Smart.Helper --version 3.5.4
                    
NuGet\Install-Package Smart.Helper -Version 3.5.4
                    
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="Smart.Helper" Version="3.5.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Smart.Helper" Version="3.5.4" />
                    
Directory.Packages.props
<PackageReference Include="Smart.Helper" />
                    
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 Smart.Helper --version 3.5.4
                    
#r "nuget: Smart.Helper, 3.5.4"
                    
#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 Smart.Helper@3.5.4
                    
#: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=Smart.Helper&version=3.5.4
                    
Install as a Cake Addin
#tool nuget:?package=Smart.Helper&version=3.5.4
                    
Install as a Cake Tool

Smart.Helper

NuGet

English | 中文

<a name="english"></a>

English

Smart.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.

Features

  • Assembly Loading: SmartAssemblyLoader for dynamically loading assemblies with dependency resolution.
  • Byte Buffer: SmartByteBuffer provides efficient, dynamic byte array manipulation with endianness support.
  • Checksums: Built-in CRC16 (Modbus) and CRC32 algorithms.
  • Caching: SmartCache implements a thread-safe, in-process memory cache with expiration support.
  • Compression: Helper methods for ZLib, GZip, and Deflate compression algorithms.
  • Data Conversion: SmartConvert offers tools for base conversion, Hex string/Byte array conversion, and more.
  • Logging: SmartLog provides a simple, asynchronous file-based logging mechanism.
  • Synchronization: SmartAutoResetEvent provides async auto-reset event with timeout and cancellation support.
  • Network: SmartNetwork includes utilities for IP/MAC address retrieval, Ping, and TCP port checking.
  • Extensions: Useful extension methods for Enums, Collections, etc.

Installation

Install the package via NuGet:

dotnet add package Smart.Helper

Usage Examples

1. SmartByteBuffer

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
2. SmartCache

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");
3. SmartConvert

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"
4. SmartNetwork

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");
5. SmartCompress

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);
6. SmartLog

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
7. SmartAutoResetEvent

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();

<a name="chinese"></a>

中文

Smart.Helper 是一个功能丰富的 .NET 工具库,包含用于数据转换、加密/校验、压缩、网络操作、日志记录等常见任务的辅助类。它支持 .NET 8, 9 和 10。

功能特性

  • 程序集加载: SmartAssemblyLoader 用于动态加载程序集并自动解析依赖项。
  • 字节缓冲区: SmartByteBuffer 提供高效、动态的字节数组操作,支持大小端切换。
  • 校验算法: 内置 CRC16 (Modbus) 和 CRC32 校验算法。
  • 缓存管理: SmartCache 实现了线程安全的进程内内存缓存,支持过期时间自动清理。
  • 数据压缩: 提供 ZLib、GZip 和 Deflate 压缩算法的便捷封装。
  • 数据转换: SmartConvert 提供进制转换、Hex 字符串与 Byte 数组互转等工具。
  • 日志记录: SmartLog 提供简单、异步的文件日志记录功能。
  • 同步原语: SmartAutoResetEvent 提供支持超时和取消令牌的异步自动重置事件。
  • 网络工具: SmartNetwork 包含 IP/MAC 地址获取、Ping 操作和 TCP 端口检测等工具。
  • 扩展方法: 针对枚举、集合等类型的实用扩展方法。

安装

通过 NuGet 安装:

dotnet add package Smart.Helper

使用示例

1. SmartByteBuffer (字节缓冲区)

高效处理二进制数据,支持自动扩容和字节序控制。

using Smart.Helper;

var buffer = new SmartByteBuffer(isLittleEndian: true);
buffer.WriteInt32(1024);
buffer.WriteString("Hello World");

byte[] data = buffer.Data; // 获取底层字节数组
2. SmartCache (内存缓存)

支持自动过期的线程安全内存缓存。

using Smart.Helper;

// 添加缓存项,5分钟后过期
SmartCache.AddOrUpdate("user_123", userObject, TimeSpan.FromMinutes(5));

// 获取缓存项
var user = SmartCache.Get<UserProfile>("user_123");
3. SmartConvert (数据转换)

处理各种数据类型的转换。

using Smart.Helper;

// Hex 字符串转 Byte 数组
byte[] bytes = SmartConvert.HexStringToByteArray("AA BB CC", " ");

// 进制转换 (例如:十六进制转二进制)
string binary = SmartConvert.ConvertBase("FF", 16, 2); // 输出 "11111111"
4. SmartNetwork (网络工具)

常用的网络操作工具。

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");
5. SmartCompress (数据压缩)

简单易用的压缩方法。

using Smart.Helper;

byte[] data = Encoding.UTF8.GetBytes("Some data to compress");

// GZip 压缩
byte[] compressed = SmartCompress.GZipCompress(data);

// GZip 解压
byte[] decompressed = SmartCompress.GZipDecompress(compressed);
6. SmartLog (日志记录)

简单的异步文件日志记录器。

using Smart.Helper;

var logger = new SmartLog("MyService");
logger.StartThread(); // 启动后台写入线程

logger.Info("服务启动成功。");
logger.Error("发生了一个错误。");

logger.StopThread(); // 刷新并停止
7. SmartAutoResetEvent (异步自动重置事件)

支持超时和取消令牌的异步自动重置事件。

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

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Smart.Helper:

Package Downloads
Smart.Modbus

Modbus协议封装库,提供ModbusRTU、ModbusTCP和ModbusASCII的组帧、响应校验、响应解析、数据缓冲,不涉及通信;分为ModbusMaster和ModbusSlave,分别用于Modbus主站和从站

Smart.Ports

Smart.Ports是一个统一的通信端口类库,提供了一致的接口来操作不同类型的通信端口,包括串口(SerialPort)、TCP客户端(TcpClient)、TCP服务端(TcpServer)和UDP节点(Udp)。该类库使用工厂模式简化端口创建,并通过继承关系提供特定端口类型的扩展功能。 Smart.Ports is a unified communication port library that provides a consistent interface for operating different types of communication ports, including SerialPort, TcpClient, TcpServer, and Udp. The library uses the factory pattern to simplify port creation and provides extended functionality for specific port types through inheritance.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.5.4 81 3/5/2026
3.5.3 79 3/5/2026
3.5.1 122 2/28/2026
3.5.0 140 2/7/2026
3.4.7 121 1/10/2026
3.4.6 110 1/10/2026
3.4.5 112 12/30/2025
3.4.4 309 10/15/2025
3.4.3 292 9/3/2025
3.4.2 325 7/20/2025
3.4.1 152 7/19/2025
3.4.0 250 7/13/2025
3.3.3 238 7/10/2025
3.3.2 418 6/8/2025
3.3.1 230 6/1/2025
3.3.0 203 6/1/2025
3.2.0 299 5/7/2025
3.1.2 205 2/28/2025
3.1.1 234 2/26/2025
3.1.0 220 2/22/2025
Loading failed