VassasCo.Utility.ConfigHelper 1.2.1

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.1
                    
NuGet\Install-Package VassasCo.Utility.ConfigHelper -Version 1.2.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="VassasCo.Utility.ConfigHelper" Version="1.2.1" />
                    
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.1" />
                    
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.1
                    
#r "nuget: VassasCo.Utility.ConfigHelper, 1.2.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 VassasCo.Utility.ConfigHelper@1.2.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=VassasCo.Utility.ConfigHelper&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=VassasCo.Utility.ConfigHelper&version=1.2.1
                    
Install as a Cake Tool

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

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

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