-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (164 loc) · 6.09 KB
/
Program.cs
File metadata and controls
206 lines (164 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) 2013-2026 Cem Dervis, MIT License.
// https://sharpconfig.org
using SharpConfig;
class SomeClass
{
public string? SomeString { get; set; }
public int SomeInt { get; set; }
public int[]? SomeInts { get; set; }
public DateTime SomeDate { get; set; }
// This field will be ignored by SharpConfig
// when creating sections from objects and vice versa.
[SharpConfig.Ignore]
#pragma warning disable CS0649 // Field is never assigned to
public int SomeIgnoredField;
#pragma warning restore CS0649
// Same for this property.
[SharpConfig.Ignore]
public int SomeIgnoredProperty { get; set; }
}
internal static class Program
{
public static void Main()
{
// Call the methods in this file here to see their effect.
// HowToLoadAConfig();
// HowToCreateAConfig();
// HowToSaveAConfig(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
// "TestCfg.ini"));
// HowToCreateObjectsFromSections();
// HowToCreateSectionsFromObjects();
// HowToHandleArrays();
}
/// <summary>
/// Shows how to load a configuration from a string (our sample config).
/// The same works for loading files and streams. Just call the appropriate method
/// such as LoadFromFile and LoadFromStream, respectively.
/// </summary>
private static void HowToLoadAConfig()
{
// Read our example config.
var cfg = Configuration.LoadFromFile("SampleCfg.txt");
// Just print all sections and their settings.
PrintConfig(cfg);
}
/// <summary>
/// Shows how to create a configuration in memory.
/// </summary>
private static void HowToCreateAConfig()
{
var cfg = new Configuration();
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
// We can obtain the values directly as strings, ints, floats, or any other (custom) type,
// as long as the string value of the setting can be converted to the type you wish to obtain.
string nameValue = cfg["SomeStructure"]["SomeString"].StringValue;
var ageValue = cfg["SomeStructure"]["SomeInt"].IntValue;
var dateValue = cfg["SomeStructure"]["SomeDate"].DateTimeValue;
// Print our config just to see that it works.
PrintConfig(cfg);
}
/// <summary>
/// Shows how to save a configuration to a file.
/// </summary>
/// <param name="filename">The destination filename.</param>
private static void HowToSaveAConfig(string filename)
{
var cfg = new Configuration();
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
cfg.SaveToFile(filename);
Console.WriteLine("The config has been saved to {0}!", filename);
}
/// <summary>
/// Shows how to create C#/.NET objects directly from sections.
/// </summary>
private static void HowToCreateObjectsFromSections()
{
var cfg = new Configuration();
// Create the section.
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
// Now create an object from it.
var p = cfg["SomeStructure"].ToObject<SomeClass>();
// Test.
Console.WriteLine("SomeString: " + p.SomeString);
Console.WriteLine("SomeInt: " + p.SomeInt);
PrintArray("SomeInts", p.SomeInts!);
Console.WriteLine("SomeDate: " + p.SomeDate);
}
/// <summary>
/// Shows how to create sections directly from C#/.NET objects.
/// </summary>
private static void HowToCreateSectionsFromObjects()
{
var cfg = new Configuration();
// Create an object.
var p = new SomeClass {
SomeString = "foobar",
SomeInt = 2000,
SomeInts = new[] { 1, 2, 3 },
SomeDate = DateTime.Now,
};
// Now create a section from it.
cfg.Add(Section.FromObject("SomeStructure", p));
// Print the config to see that it worked.
PrintConfig(cfg);
}
/// <summary>
/// Shows the usage of arrays in SharpConfig.
/// </summary>
private static void HowToHandleArrays()
{
var cfg = new Configuration();
cfg["GeneralSection"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
// Get the array back.
var someIntValuesBack = cfg["GeneralSection"]["SomeInts"].GetValueArray<int>();
var sameValuesButFloats = cfg["GeneralSection"]["SomeInts"].GetValueArray<float>();
var sameValuesButStrings = cfg["GeneralSection"]["SomeInts"].GetValueArray<string>();
// There is also a non-generic variant of GetValueArray:
var sameValuesButObjects = cfg["GeneralSection"]["SomeInts"].GetValueArray(typeof(int));
PrintArray("someIntValuesBack", someIntValuesBack!);
PrintArray("sameValuesButFloats", sameValuesButFloats!);
PrintArray("sameValuesButStrings", sameValuesButStrings!);
PrintArray("sameValuesButObjects", sameValuesButObjects!);
}
/// <summary>
/// Prints all sections and their settings to the console.
/// </summary>
/// <param name="cfg">The configuration to print.</param>
private static void PrintConfig(Configuration cfg)
{
foreach (var section in cfg)
{
Console.WriteLine("[{0}]", section.Name);
foreach (Setting setting in section)
{
Console.Write(" ");
if (setting.IsArray)
{
Console.Write("[Array, {0} elements] ", setting.ArraySize);
}
Console.WriteLine(setting.ToString());
}
Console.WriteLine();
}
}
// Small helper method for printing objects of an arbitrary type.
private static void PrintArray<T>(string arrName, IReadOnlyList<T> arr)
{
Console.Write(arrName + " = { ");
for (int i = 0; i < arr.Count - 1; i++)
{
Console.Write(arr[i] + ", ");
}
Console.Write(arr[^1]!.ToString());
Console.WriteLine(" }");
}
}