Skip to content

基础示例

本页展示 Apq.Cfg 的基础用法示例。

读取配置值

配置文件

json
{
  "App": {
    "Name": "MyApplication",
    "Version": "1.0.0",
    "Debug": true
  },
  "Database": {
    "Host": "localhost",
    "Port": 5432,
    "Database": "mydb",
    "Timeout": 30
  },
  "Features": {
    "EnableCache": true,
    "CacheSize": 1000,
    "CacheExpiry": "00:30:00"
  }
}

读取字符串

csharp
var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)
    .Build();

// 使用 Get 方法
var appName = cfg.Get("App:Name");
Console.WriteLine($"应用名称: {appName}");

// 嵌套路径
var dbHost = cfg.Get("Database:Host");
Console.WriteLine($"数据库主机: {dbHost}");

读取类型化值

csharp
// 整数
var port = cfg.Get<int>("Database:Port");
Console.WriteLine($"端口: {port}");

// 布尔值
var debug = cfg.Get<bool>("App:Debug");
Console.WriteLine($"调试模式: {debug}");

// 超时
var timeout = cfg.Get<int>("Database:Timeout");
Console.WriteLine($"超时: {timeout}秒");

配置节操作

获取配置节

csharp
var dbSection = cfg.GetSection("Database");

Console.WriteLine($"主机: {dbSection.Get("Host")}");
Console.WriteLine($"端口: {dbSection.Get<int>("Port")}");
Console.WriteLine($"数据库: {dbSection.Get("Database")}");

遍历子键

csharp
var appSection = cfg.GetSection("App");
foreach (var key in appSection.GetChildKeys())
{
    Console.WriteLine($"{key} = {appSection.Get(key)}");
}

检查配置存在

csharp
if (cfg.Exists("Optional"))
{
    Console.WriteLine("可选配置存在");
}
else
{
    Console.WriteLine("可选配置不存在");
}

强类型绑定

定义配置类

csharp
public class AppConfig
{
    public string Name { get; set; } = "";
    public string Version { get; set; } = "";
    public bool Debug { get; set; }
}

public class DatabaseConfig
{
    public string Host { get; set; } = "localhost";
    public int Port { get; set; } = 5432;
    public string Database { get; set; } = "";
    public int Timeout { get; set; } = 30;
}

public class FeaturesConfig
{
    public bool EnableCache { get; set; }
    public int CacheSize { get; set; }
    public TimeSpan CacheExpiry { get; set; }
}

绑定配置

csharp
// 手动绑定
var appSection = cfg.GetSection("App");
var appConfig = new AppConfig
{
    Name = appSection.Get("Name") ?? "",
    Version = appSection.Get("Version") ?? "",
    Debug = appSection.Get<bool>("Debug")
};

var dbSection = cfg.GetSection("Database");
var dbConfig = new DatabaseConfig
{
    Host = dbSection.Get("Host") ?? "localhost",
    Port = dbSection.Get<int>("Port"),
    Database = dbSection.Get("Database") ?? "",
    Timeout = dbSection.Get<int>("Timeout")
};

Console.WriteLine($"应用: {appConfig.Name} v{appConfig.Version}");
Console.WriteLine($"数据库: {dbConfig.Host}:{dbConfig.Port}/{dbConfig.Database}");

数组配置

配置文件

json
{
  "Servers": [
    "server1.example.com",
    "server2.example.com",
    "server3.example.com"
  ],
  "Endpoints": [
    {
      "Name": "api",
      "Url": "https://api.example.com",
      "Timeout": 30
    },
    {
      "Name": "auth",
      "Url": "https://auth.example.com",
      "Timeout": 10
    }
  ]
}

读取数组

csharp
// 字符串数组
var serversSection = cfg.GetSection("Servers");
foreach (var key in serversSection.GetChildKeys())
{
    Console.WriteLine($"服务器: {serversSection.Get(key)}");
}

// 对象数组
public class EndpointConfig
{
    public string Name { get; set; } = "";
    public string Url { get; set; } = "";
    public int Timeout { get; set; }
}

var endpointsSection = cfg.GetSection("Endpoints");
foreach (var key in endpointsSection.GetChildKeys())
{
    var endpoint = endpointsSection.GetSection(key);
    Console.WriteLine($"端点: {endpoint.Get("Name")} -> {endpoint.Get("Url")}");
}

// 按索引访问
var firstServer = cfg.Get("Servers:0");
var firstEndpointName = cfg.Get("Endpoints:0:Name");

字典配置

配置文件

json
{
  "ConnectionStrings": {
    "Default": "Server=localhost;Database=default;",
    "Readonly": "Server=readonly;Database=default;",
    "Analytics": "Server=analytics;Database=stats;"
  }
}

读取字典

csharp
var connSection = cfg.GetSection("ConnectionStrings");
foreach (var key in connSection.GetChildKeys())
{
    Console.WriteLine($"{key}: {connSection.Get(key)}");
}

可写配置

csharp
var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: true, isPrimaryWriter: true)
    .Build();

// 修改配置
cfg.Set("App:Name", "NewName");
cfg.Set("Database:Port", "5433");

// 保存到文件
await cfg.SaveAsync();

完整示例

csharp
using Apq.Cfg;

// 创建配置
var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)
    .Build();

// 读取应用配置
var appSection = cfg.GetSection("App");
Console.WriteLine($"=== {appSection.Get("Name")} v{appSection.Get("Version")} ===");

// 读取数据库配置
var dbSection = cfg.GetSection("Database");
Console.WriteLine($"数据库: {dbSection.Get("Host")}:{dbSection.Get<int>("Port")}");

// 读取功能配置
var featuresSection = cfg.GetSection("Features");
if (featuresSection.Get<bool>("EnableCache"))
{
    Console.WriteLine($"缓存已启用,大小: {featuresSection.Get<int>("CacheSize")}");
}

// 遍历所有顶级配置
Console.WriteLine("\n所有配置节:");
var rootSection = cfg.GetSection("");
foreach (var key in rootSection.GetChildKeys())
{
    Console.WriteLine($"  - {key}");
}

下一步

基于 MIT 许可发布