EasyConfig.Json 1.0.0

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

EasyConfig.Json

一个简单易用的 .NET 配置管理库,支持通过特性标记属性并自动持久化到 JSON 文件。

✨ 特性

  • 🎯 简单易用:通过特性标记需要持久化的属性
  • 💾 自动持久化:自动加载和保存配置到 JSON 文件
  • 🔒 线程安全:内置锁机制,支持多线程环境
  • 🏷️ 自定义键名:支持自定义配置键名或使用默认的"类名.属性名"格式
  • 📝 UTF-8 友好:支持中文等 Unicode 字符
  • 🔄 批量管理:支持注册多个对象并统一管理配置

📦 安装

通过 NuGet 安装:

dotnet add package EasyConfig.Json

或在 Package Manager Console 中:

Install-Package EasyConfig.Json

🚀 快速开始

1. 标记需要持久化的属性

使用 [ConfigProperty] 特性标记需要持久化的属性:

using EasyConfig.Attributes;

public class AppSettings
{
    // 使用默认键名: "AppSettings.Username"
    [ConfigProperty]
    public string Username { get; set; } = "DefaultUser";

    // 使用自定义键名: "user_age"
    [ConfigProperty("user_age")]
    public int Age { get; set; } = 18;

    [ConfigProperty]
    public bool IsEnabled { get; set; } = true;

    // 未标记的属性不会被持久化
    public string TempValue { get; set; } = "Not Saved";
}

2. 基本使用示例

// 【预留给您编写示例代码】
// 请在此处添加您的使用示例

📖 API 文档

ConfigManager 类

静态配置管理器,提供配置的加载和保存功能。

方法
LoadConfig(object target, string filePath = "settings.json")

加载配置文件并应用到目标对象的标记属性。

参数:

  • target:目标对象
  • filePath:配置文件路径,默认为 "settings.json"

示例:

// 【预留给您编写示例代码】
SaveAllConfigs(string filePath = "settings.json")

保存所有已注册对象的配置到文件。

参数:

  • filePath:配置文件路径,默认为 "settings.json"

返回值:

  • bool:保存成功返回 true,失败返回 false

示例:

// 【预留给您编写示例代码】
GetRegisteredCount()

获取已注册对象的数量。

返回值:

  • int:已注册对象的数量
ClearRegistrations()

清空所有已注册的对象(主要用于测试)。

ConfigPropertyAttribute 特性

标记需要持久化的属性。

构造函数
public ConfigPropertyAttribute(string? customKey = null)

参数:

  • customKey:自定义配置键名,如果为 null 则使用"类名.属性名"格式

💡 使用场景

  • 应用程序配置管理
  • 用户偏好设置
  • 游戏存档
  • 应用状态持久化
  • 任何需要简单配置管理的场景

📝 配置文件格式

配置文件为 JSON 格式,例如:

{
  "AppSettings.Username": "John",
  "user_age": 25,
  "AppSettings.IsEnabled": true
}

🔧 高级特性

线程安全

ConfigManager 内置了线程安全机制,可以在多线程环境中安全使用:

// 【预留给您编写示例代码】

批量管理多个配置对象

// 【预留给您编写示例代码】

⚠️ 注意事项

  1. 只有标记了 [ConfigProperty] 的属性才会被持久化
  2. 属性必须具有 public getter 和 setter
  3. 配置文件使用 UTF-8 编码
  4. 调用 LoadConfig 时会自动注册对象到管理器
  5. 保存配置时会保存所有已注册对象的配置

📄 许可证

本项目采用 MIT 许可证。

🔗 相关链接

🤝 贡献

欢迎提交 Issue 和 Pull Request!


Made with ❤️ by Hu

Product Compatible and additional computed target framework versions.
.NET 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 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.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 300 11/13/2025