VassasCo.Utility.ConfigHelper 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package VassasCo.Utility.ConfigHelper --version 1.2.0
                    
NuGet\Install-Package VassasCo.Utility.ConfigHelper -Version 1.2.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="VassasCo.Utility.ConfigHelper" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VassasCo.Utility.ConfigHelper" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="VassasCo.Utility.ConfigHelper" />
                    
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 VassasCo.Utility.ConfigHelper --version 1.2.0
                    
#r "nuget: VassasCo.Utility.ConfigHelper, 1.2.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 VassasCo.Utility.ConfigHelper@1.2.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=VassasCo.Utility.ConfigHelper&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.0
                    
Install as a Cake Tool

ConfigHelper

零代码实体类与 JSON/XML 配置文件双向映射工具。支持特性标注和非侵入式 Fluent API 两种方式。

文件说明

文件 内容
ConfigAttributes.cs 特性定义:[JsonConfig]、[XmlConfig]、[Config]、[ConfigIgnore] 等
ConfigCore.cs 核心逻辑:ConfigHelper<T>、ConfigFactory、ConfigBase<T>、ConfigRawReader、ConfigManager
ConfigProfile.cs 非侵入式 Fluent API 映射(实体类零特性)
ConfigSerializers.cs 序列化器:JsonCommentWriter、XmlCommentWriter、XmlConfigHelper
ConfigExtensions.cs 实体类扩展方法:EnsureListsInitialized、EnsureDefaultsForNulls
ConfigConverters.cs 内置转换器:HexInt、TimeSpan、DateTime、BoolFormat 等

快速开始

方式 1:特性标注(侵入式,适合配置类自己定义)

[JsonConfig("config/app.json")]
public class AppConfig : ConfigBase<AppConfig>
{
    [Config(Desc = "应用名称", Default = "MyApp", Required = true)]
    public string AppName { get; set; } = "";

    [Config(Desc = "端口号", Default = 8080)]
    public int Port { get; set; }

    [Config(Desc = "服务器列表")]
    public List<ServerInfo> Servers { get; set; } = new();
}

public class ServerInfo
{
    [Config(Desc = "服务器IP")]
    public string Ip { get; set; } = "";
    public int Port { get; set; }
}

方式 2:ConfigProfile(非侵入式,实体类零特性)

适用于不想让实体类引用配置库的场景(如共享 DTO、第三方 Model):

// 实体类 — 零特性,干净
public class OrderModel
{
    public string OrderType { get; set; } = "";
    public string ChannelNo { get; set; } = "";
    public string ProductCode { get; set; } = "";
    public string ProductName { get; set; } = "";
    public int Quantity { get; set; }
}

// 映射配置 — 写在外部
var profile = new ConfigProfile<OrderModel>("config/order.json")
    .Map(o => o.OrderType,   key: "订货类型", desc: "订货类型", required: true)
    .Map(o => o.ChannelNo,   key: "渠道编号", desc: "渠道唯一编号")
    .Map(o => o.ProductCode, key: "货号",     desc: "商品编码")
    .Map(o => o.ProductName, key: "货品名称")
    .Map(o => o.Quantity,    key: "数量",     defaultValue: 0);

// 构建 ConfigHelper,后续使用完全一致
var helper = profile.Build();
var order = helper.Value;
helper.Save();
helper.EnableHotReload();

全局注册(Profile 模式也可用)

// 启动时注册一次
var helper = profile.Build();
ConfigManager.Register(helper);

// 任意线程、任意类中获取
var helper = ConfigManager.GetHelper<OrderModel>();
var order = helper.Value;
helper.Update(cfg => cfg.Quantity = 100);
helper.Save();

注意:[Description] 特性不会被自动导入为注释。如需注释,请使用 [Config(Desc="xxx")] 或 ConfigProfile.Map(desc: "xxx")。

加载与保存

// ConfigFactory 入口
var cfg = ConfigFactory.Load<AppConfig>();
ConfigFactory.Save(cfg);

// ConfigBase<T> 基类
var cfg = AppConfig.Current;

// 手动创建 ConfigHelper
var helper = new ConfigHelper<MyConfig>("config/my.json", ConfigFormat.Json);
var cfg = helper.Value;
helper.Save();
// 跨类获取(需先 Register)
var helper = ConfigManager.GetHelper<MyConfig>();

热重载

var helper = ConfigFactory.GetHelper<AppConfig>();
helper.EnableHotReload();  // 配置文件外部修改后自动重载

Update 原子修改

helper.Update(cfg =>
{
    cfg.AppName = "新名称";
    cfg.Servers.Add(new ServerInfo { Ip = "10.0.0.1" });
});
helper.Save();

事件监听

helper.ConfigChanged += (s, e) =>
{
    Console.WriteLine($"配置变更: {e.ChangeType}");
};
helper.ConfigSaving += (s, e) =>
{
    if (!Validate(e.Config)) e.Cancel = true; // 可取消保存
};

嵌套类型映射(ConfigProfile)

var profile = new ConfigProfile<AppConfig>("config/app.json")
    .Map(o => o.AppName, desc: "应用名")
    // 单个嵌套类
    .Nested<DatabaseConfig>(db => db
        .Map(d => d.Host, desc: "主机")
        .Map(d => d.Port, desc: "端口"))
    .Map(o => o.Database, desc: "数据库配置")
    // List<嵌套类>
    .Nested<ServerInfo>(si => si
        .Map(s => s.Ip, desc: "IP地址")
        .Map(s => s.Name, desc: "名称"))
    .Map(o => o.Servers, desc: "服务器列表");

var helper = profile.Build();

特性说明

类级特性

特性 说明
[JsonConfig("path")] 标记为 JSON 配置
[XmlConfig("path")] 标记为 XML 配置

属性特性 [Config]

参数 类型 说明
Desc string 属性描述,写入注释
Key string 自定义 JSON/XML 键名
Default object 默认值
Ignore bool 忽略此属性
Required bool 必填校验
StringLength int 字符串最大长度
Converter Type 自定义转换器(实现 IConfigConverter)
ConverterArgs object[] 转换器构造参数

ConfigProfile.Map() 参数对照

特性参数 Map() 参数 说明
[Config(Desc="")] desc: 注释描述
[Config(Key="")] key: 自定义键名
[Config(Default=)] defaultValue: 默认值
[Config(Ignore=true)] 不调 .Map() 即是忽略 不注册的属性不会被序列化
[Config(Required=true)] required: 必填校验
[Config(StringLength=)] stringLength: 字符串最大长度
[Config(Converter=)] converterType: 自定义转换器
[Config(ConverterArgs=)] converterArgs: 转换器构造参数

内置转换器

  • HexIntConverter - 十六进制
  • TimeSpanStringConverter - 时间跨度
  • DateTimeStringConverter - 日期时间
  • BoolFormatConverter("是","否") - 布尔格式化
  • NumberFormatConverter("F2") - 数字格式化
  • VersionConverter - 版本号
  • TypeNameConverter - 类型名
  • EnumIntConverter - 枚举存数字

XML 配置

[XmlConfig("config/settings.xml", RootName = "Settings")]
public class XmlSettings : ConfigBase<XmlSettings>
{
    [Config(Desc = "服务名", Key = "ServiceName")]
    public string Name { get; set; } = "";
}

ConfigRawReader — 无需实体类直接读值

var ip = ConfigRawReader.GetJsonValue("config.json", "device.ip");
var name = ConfigRawReader.GetXmlValue("config.xml", "/AppSettings/ServiceName");

路径支持:"servers[0].name"、"nested.deep.key"

兼容性

  • .NET Standard 2.0
  • .NET 6 / .NET 8 / .NET 9 / .NET 10
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 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

  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on VassasCo.Utility.ConfigHelper:

Package Downloads
VassasCo.Utility

C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 87 10/12/2026
2.0.0 112 9/23/2026
1.2.1 160 8/18/2026
1.2.0 230 6/28/2026
1.1.0 233 6/18/2026