VassasCo.Utility.ConfigHelper
1.2.1
See the version list below for details.
dotnet add package VassasCo.Utility.ConfigHelper --version 1.2.1
NuGet\Install-Package VassasCo.Utility.ConfigHelper -Version 1.2.1
<PackageReference Include="VassasCo.Utility.ConfigHelper" Version="1.2.1" />
<PackageVersion Include="VassasCo.Utility.ConfigHelper" Version="1.2.1" />
<PackageReference Include="VassasCo.Utility.ConfigHelper" />
paket add VassasCo.Utility.ConfigHelper --version 1.2.1
#r "nuget: VassasCo.Utility.ConfigHelper, 1.2.1"
#:package VassasCo.Utility.ConfigHelper@1.2.1
#addin nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.1
#tool nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.1
ConfigHelper
零代码实体类与 JSON/XML 配置文件双向映射工具。
文件说明
| 文件 | 内容 |
|---|---|
ConfigAttributes.cs |
特性定义:[JsonConfig]、[XmlConfig]、[Config]、[ConfigIgnore] 等 |
ConfigCore.cs |
核心逻辑:ConfigHelper<T>、ConfigFactory、ConfigBase<T>、ConfigRawReader、JsonCommentWriter、XmlCommentWriter、XmlConfigHelper |
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. 加载/保存
// 方式 1:ConfigFactory 入口
var cfg = ConfigFactory.Load<AppConfig>();
ConfigFactory.Save(cfg);
// 方式 2:ConfigBase<T> 基类
var cfg = AppConfig.Current;
3. 手动创建实例
var helper = new ConfigHelper<MyConfig>("config/my.json", ConfigFormat.Json);
var cfg = helper.Value;
helper.Save();
4. 热重载
var helper = ConfigFactory.GetHelper<AppConfig>();
helper.EnableHotReload(); // 配置文件外部修改后自动重载
5. 事件监听
helper.ConfigChanged += (s, e) =>
{
Console.WriteLine($"配置变更: {e.ChangeType}");
};
helper.ConfigSaving += (s, e) =>
{
if (!Validate(e.Config)) e.Cancel = true; // 可取消保存
};
特性说明
类级特性
| 特性 | 说明 |
|---|---|
[JsonConfig("path")] |
标记为 JSON 配置 |
[XmlConfig("path")] |
标记为 XML 配置 |
属性特性
| 参数 | 类型 | 说明 |
|---|---|---|
Desc |
string | 属性描述,写入注释 |
Key |
string | 自定义 JSON/XML 键名 |
Default |
object | 默认值 |
Ignore |
bool | 忽略此属性 |
Required |
bool | 必填校验 |
StringLength |
int | 字符串最大长度 |
Converter |
Type | 自定义转换器 |
内置转换器
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"
| 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 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. |
| .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)
-
net6.0
- No dependencies.
-
net8.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.