VassasCo.Utility.ConfigHelper
1.2.0
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
<PackageReference Include="VassasCo.Utility.ConfigHelper" Version="1.2.0" />
<PackageVersion Include="VassasCo.Utility.ConfigHelper" Version="1.2.0" />
<PackageReference Include="VassasCo.Utility.ConfigHelper" />
paket add VassasCo.Utility.ConfigHelper --version 1.2.0
#r "nuget: VassasCo.Utility.ConfigHelper, 1.2.0"
#:package VassasCo.Utility.ConfigHelper@1.2.0
#addin nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.0
#tool nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.0
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 | Versions 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. |
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.4)
-
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.