using System;
using ConfigurationPattern;
using ConfigurationPattern.Patterns;
namespace CfgSample
{
[ConfigurationPattern(TPattern.INI, "DefaultSection")]
class MyIniCfg : Configuration
{
const string MY_INI_CFG_PATH = @"\myXMLcfg.ini";
public MyIniCfg() : base(MY_INI_CFG_PATH)
{
m_RandId = new Random().Next();
}
private string m_Name;
private byte m_Age;
private int m_RandId;
private string m_ClassName;
private uint m_Year;
#region Public Properties
[Section("PrivateInfo")]
public string Name
{
get => m_Name;
set => m_Name = value;
}
[Section("PrivateInfo")]
[Key("Age", 18)]
public byte Age
{
get => m_Age;
set => m_Age = value;
}
[Section("ClassInfo")]
[Key("Class")]
public string ClassName
{
get => m_ClassName;
set => m_ClassName = value;
}
[Section("ClassInfo")]
[Key("Year", 1945)]
public uint Year
{
get => m_Year;
set => m_Year = value;
}
[ConfigurationIgnore]
public int RandId
{
get => m_RandId;
set => m_RandId = value;
}
#endregion
}
}
Name、Age 歸屬 [PrivateInfo] 區段ClassName、Year 歸屬 [ClassInfo] 區段[Section] 的屬性自動歸入建構式指定的 DefaultSection| 屬性 | INI 鍵名 | 預設值 | 說明 |
|---|---|---|---|
| Name | Name | null | 未指定 Key,採用屬性名 |
| Age | Age | 18 | 明確指定預設值 |
| ClassName | Class | null | 自訂鍵名異於屬性名 |
| Year | Year | 1945 | 數值型別預設值 |
RandId 標記為忽略,不會出現在 INI 檔案中,適合:
[PrivateInfo]
Name=John
Age=25
[ClassInfo]
Class=ComputerScience
Year=2023
[DefaultSection]
; 其他未分組屬性
元件內建支援所有基元型別(int, byte, uint, string, bool 等),複雜型別可透過 TypeConverter 或自訂序列化擴展。