LuBan.Common 2026.7.17.1

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

English | 中文

LuBan.Common

作者: yswenli | 联系邮箱: yswenli@outlook.com | 代码仓库: https://github.com/yswenli/luban-framework

"企业级开发的瑞士军刀,开箱即用,少写 80% 的样板代码。"

你是否还在为这些琐事头疼?

  • Excel 导入导出又要手写一遍?
  • HTTP 请求想简单调个接口却层层封装?
  • 缓存、日志、文件、字符串、日期、IP、二维码……每个项目都要重新造一遍轮子?

如果是,那 LuBan.Common 就是为你准备的。

它是一个基于 .NET 8 的通用工具类库,把企业开发里最高频、最琐碎的需求打包成一套顺手工具:从 Excel、HTTP、文件 IO、缓存、日志,到短信、图像、二维码、IP 归属地、拼音转换,应有尽有。它不是框架,而是你的 "开发外挂" —— 直接引用,马上开工。


Related Projects: LuBan.Framework | LuBan.Threading | LuBan.DI | LuBan.Orm | LuBan.Web.Core | LuBan.Redis | LuBan.EventBus


为什么选择 LuBan.Common?

痛点 LuBan.Common 的解法
Excel/CSV 处理繁琐 ExcelUtilCsvUtil 一行导入导出
HTTP 调用封装复杂 HttpClientProxy + 字符串扩展, GET/POST 像呼吸一样自然
缓存没有过期策略 MemoryCache<T> 自带定时清理
日志配置没完没了 Logger 一句打印,自动接管
文件读写、路径处理总写错 FileUtilPathUtil 把常见操作封成静态方法
需要短信、二维码、IP 定位 SmsSenderImageUtilIPToRegion 直接可用

一分钟预览

using LuBan.Common;
using LuBan.Common.Data;
using LuBan.Common.IO;

// Excel 秒读
using var fs = File.OpenRead("orders.xlsx");
var dt = ExcelUtil.ImportFromStream(fs, v2003: false, sheetName: "sheet1");

// HTTP 秒调
var proxy = new HttpClientProxy();
var json = await proxy.GetAsync("https://api.example.com/data");

// 缓存秒存
var cache = MemoryCache<string>.GetInstance();
cache.Set("user:1001", "active", TimeSpan.FromMinutes(10));

// 文件秒写
FileUtil.WriteString("log.txt", "保存成功");

// 日志秒打
Logger.Info("订单模块", "订单创建成功");

没有废话,只有生产力。


技术栈

  • 目标框架:.NET 8.0
  • 项目类型:类库(Class Library)
  • NuGet 包LuBan.Common

安装

dotnet add package LuBan.Common

然后在你需要的地方 using 对应的命名空间即可开工。


工具箱全览

字符串与数据转换

StringUtilStringPlusBase64UtilHexConverterNumberUtilDecimalUtilGuidUtilCodeUtilPasswordUtilModelUtilDynamicUtilAnonymousTypeUtil

字符串处理、单位换算、编码解码、密码校验、对象映射……能想到的转换,基本都有。

日期时间

DateTimeUtil + Calendar.ChineseCalendar

格式化、工作日计算、中文星期、农历、节气、生肖,全搞定。

集合与数据结构

CollectionUtilLinqExtentionSmartListPagedListPagedDictionaryThreadSafeListBatcherDataSplitter

分页、线程安全、批量、拆分,集合操作的常见痛点一次解决。

Excel / CSV

Data.ExcelUtilData.CsvUtilDataTableUtil

Excel 导入导出,流式读取,直接出 DataTable

缓存

MemoryCache<T>IServiceCacheLocalCacheUtil

MemoryCache<T> 自带过期清理,支持泛型;LocalCacheUtil 持久化到本地文件。

注意:MemoryCache<T>IServiceCacheLogger 位于 System 命名空间。

HTTP 通信

HttpClientProxyHttp.HttpExtensionHttp.FormUploadHttp.FileParameter

支持 GET / POST / 上传 / 自定义 Header,字符串还能直接 .HttpGetAsync().HttpPostAsync()

文件 IO

IO.FileUtilIO.PathUtilIO.FileTypeUtilIO.TempFileIO.PersistenceFile

文件读写、路径处理、临时文件、序列化存储,常见操作一行代码。

日志

Logger(命名空间 System)、LogInfoApiLogInfo

静态调用:Logger.InfoLogger.ErrorLogger.Debug,省事到离谱。

系统与进程

ComputerUtilProcessUtilShellUtilEnvironmentArgsUtilRuntimeUtilServiceUtilCallContext

机器信息、进程管理、Shell 执行、环境参数、运行时信息,系统级操作也不在话下。

反射与高性能

ReflectionUtilFastILUtilFastCopyEnumUtilBaseSingleInstance<T>

反射不卡、枚举好读、单例基类随拿随用。

网络与 IP

IPUtilLANUtilIPToRegion.IPRegion

IP 归属地查询基于 ip2region,本地离线解析,无需联网。

图像与二维码

ImageUtil、二维码生成与识别

图片处理、二维码生成与识别,直接嵌入业务代码。

短信服务

Sms.SmsSenderSms.SmsOption

短信发送抽象,配置化接入。

安全与防重放

AraReplayAttacksUtilAntiReplayAttacks.AraInfoPasswordUtil

接口安全,防重放攻击,密码加密校验。

双因素认证 (MFA/2FA)

OptUtil

TOTP(Time-based One-Time Password)双因素认证,生成验证码、校验验证码、生成 QR 码供认证器应用扫描。

支持 Google Authenticator、Microsoft Authenticator 等标准认证器应用。

快速上手:

using LuBan.Common;

// 1. 生成密钥(用户启用 MFA 时)
var secret = OptUtil.GenerateSecret();

// 2. 生成 QR 码供用户扫描
var qrCode = OptUtil.GenerateQrCode(secret, "user@example.com", "MyApp");

// 3. 验证用户输入的动态码
var userInput = "123456"; // 用户从认证器应用获取的 6 位数字
if (OptUtil.ValidateTotp(userInput, secret))
{
    // 验证成功
}

配置与常量

ConfigUtilNacosConfigUtilConsts.*

配置文件、配置中心、项目常量,统一管理。

错误处理

Errors.FriendlyExceptionErrors.EnumErrorCode

业务异常规范化,前端拿到错误码,后端抛出友好提示。

事件总线接口

EventBus.IEventBusIEventDataIEventHandler<T>

定义事件总线契约,具体实现可由上层框架注入。

其他宝藏

SerializeUtilStreamUtilZipUtilTracerTryUtilTokenUtilEmailUtilConsoleUtilNPinyin.Pinyin

序列化、流、压缩、追踪、Token、邮件、控制台、拼音转换……这里藏了不少小惊喜。


实战示例

Excel 导入:从混乱到清晰

using LuBan.Common.Data;
using System.Data;

DataTable? dt;
using (var fs = File.OpenRead("orders.xlsx"))
{
    dt = ExcelUtil.ImportFromStream(fs, v2003: false, sheetName: "sheet1", hasHeader: true);
}

没有样板代码,只有一行核心调用。

HTTP 请求:像写本地方法一样写远程调用

using LuBan.Common;
using LuBan.Common.Http;

var client = new HttpClientProxy();

// 普通 GET
string html = await client.GetAsync("https://api.example.com/data");

// 泛型反序列化
var user = await client.GetAsync<UserDto>("https://api.example.com/user/1");

// POST 对象,自动转 JSON
var result = await client.PostAsync("https://api.example.com/order", new { Id = 1, Name = "VIP" });

// 更懒的写法:字符串扩展
string data = await "https://api.example.com/data".HttpGetAsync();

缓存:自带过期,不用管GC

using System;

var cache = MemoryCache<string>.GetInstance();

cache.Set("session:123", "user-data", TimeSpan.FromMinutes(30));

var value = cache.Get("session:123");

文件操作:读写、序列化、删除一把梭

using LuBan.Common.IO;

// 读文本
string content = FileUtil.ReadString("readme.txt");

// 写文本
FileUtil.WriteString("output.txt", "Hello LuBan!");

// 对象存文件
FileUtil.Write("config.json", new { Host = "localhost", Port = 5000 });

// 读对象
var config = FileUtil.Read<Config>("config.json");

// 文件存在吗?
bool exists = FileUtil.Exists("output.txt");

日志:三行配置,一行打印

using System;

Logger.Info("订单模块", "订单 10086 创建成功");
Logger.Error("支付模块", exception, "支付回调处理失败");
Logger.Debug("调试", $"耗时 {elapsedMs}ms");

使用前请确保项目根目录有日志配置文件。

日期时间:告别手撕时间逻辑

using LuBan.Common;

var now = DateTimeUtil.Now;
string weekday = DateTimeUtil.GetChineseWeekDay(now);     // 星期四
int workDays = DateTimeUtil.GetWorkDays(now, now.AddDays(10));

业务异常:让错误也优雅

using LuBan.Common.Errors;

try
{
    throw new FriendlyException(EnumErrorCode.InvalidParameter, "手机号格式不正确");
}
catch (FriendlyException ex)
{
    Console.WriteLine($"[{ex.ErrorCode}] {ex.Message}");
}

事件总线:接口先行,实现灵活

using LuBan.Common.EventBus;

public class UserCreatedEvent : IEventData
{
    public string UserId { get; set; } = string.Empty;
    public string UserName { get; set; } = string.Empty;
}

public class UserCreatedHandler : IEventHandler<UserCreatedEvent>
{
    public Task HandleAsync(UserCreatedEvent e)
    {
        Console.WriteLine($"欢迎新用户:{e.UserName}");
        return Task.CompletedTask;
    }
}

// IEventBus 的具体实现由上层框架或业务项目注入

双因素认证 (MFA):企业级安全标配

using LuBan.Common;

// 生成密钥并返回 QR 码供用户扫描
var secret = OptUtil.GenerateSecret();
var qrCodeImage = OptUtil.GenerateQrCode(secret, "user@example.com", "MyApp");

// 将 QR 码返回给前端(ASP.NET Core Controller)
// return File(qrCodeImage, "image/png");

// 验证用户输入的动态码(±30秒时间容差)
var userCode = "123456";
if (OptUtil.ValidateTotp(userCode, secret))
{
    Console.WriteLine("MFA 验证成功");
}

// 或使用已有的 RandomUtil 方法
if (RandomUtil.ValideWFACode(userCode, secret))
{
    Console.WriteLine("MFA 验证成功");
}

使用小贴士

  1. 命名空间别搞错LuBan.Common 是根命名空间,但具体工具分散在 LuBan.Common.DataLuBan.Common.IOLuBan.Common.HttpLuBan.Common.SmsLuBan.Common.ErrorsLuBan.Common.EventBusLuBan.Common.CalendarLuBan.Common.ConstsLuBan.Common.IPToRegionLuBan.Common.AntiReplayAttacksLuBan.Common.NPinyin 等子命名空间;MemoryCache<T>IServiceCacheLogger 位于 System
  2. 日志先配置Logger 依赖日志配置文件,否则可能找不到输出目标。
  3. 线程安全要选型:多线程场景优先 ThreadSafeList、并发字典或 MemoryCache<T>
  4. 缓存设过期:缓存不是无限内存,请按业务设置合理过期时间。
  5. 配置走 Nacos:经常变化的配置用 NacosConfigUtil,避免改配置就发版。

许可证

MIT License


LuBan.Common —— 让 .NET 企业开发更简单、更快、更爽。

如果你也觉得重复造轮子很无聊,那就把它加进你的工具箱吧。

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.  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 (20)

Showing the top 5 NuGet packages that depend on LuBan.Common:

Package Downloads
LuBan.Linq

LuBan.Framework中动态linq的处理库

LuBan.DI

LuBan.framework中依赖注入核心库

LuBan.Orm

LuBan Framework中ORM核心,基于YitIdHelper、Mapster、SqlSugar封装

LuBan.VideoKit

企服版视频处理工具元件

LuBan.EventBus

LuBan框架中的事件总线,让业务模块之间像发微信一样简单沟通,告别"你找我、我找它"的混乱局面

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.7.17.1 90 7/17/2026
2026.7.13.2 490 7/13/2026
2026.7.13.1 507 7/13/2026
2026.7.12.2 511 7/12/2026
2026.7.12.1 514 7/12/2026
2026.7.11.2 523 7/11/2026

LuBan Framework工具类项目,三方引用包主要有:Encrypt.Library、HtmlAgilityPack、ICSharpCode.SharpZipLib、log4net、Newtonsoft.Json、NPOI、SkiaSharp、ZXing等