-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.ConfigItem.cs
More file actions
45 lines (41 loc) · 1.5 KB
/
Config.ConfigItem.cs
File metadata and controls
45 lines (41 loc) · 1.5 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
using System;
namespace Teto {
public partial class Config {
/// <summary>
/// A configuration item.
/// </summary>
public class ConfigItem {
/// <summary>
/// The original value, as in the .cfg file.
/// </summary>
public string StringValue { get; private set; }
/// <summary>
/// Construct a config item.
/// </summary>
/// <param name="value">The string representation of the value.</param>
public ConfigItem(string value) {
StringValue = value;
}
/// <summary>
/// Get a config item as object of type T.
/// </summary>
/// <typeparam name="T">The type of object to return.</typeparam>
/// <returns>The object as type T.</returns>
public T Value<T>() {
if (typeof(T) == typeof(int)) {
return (T)(object)int.Parse(StringValue);
}
if (typeof(T) == typeof(long)) {
return (T)(object)long.Parse(StringValue);
}
if (typeof(T) == typeof(bool)) {
return (T)(object)(StringValue.ToLower() == "true");
}
if (typeof(T) == typeof(string)) {
return (T)(object)StringValue;
}
throw new ArgumentException("The requested type is not valid.");
}
}
}
}