Create DictionaryBase to hold helpers

This commit is contained in:
Matt Nadareski
2023-08-07 21:10:47 -04:00
parent a45810aed6
commit 4dffd76574
3 changed files with 49 additions and 121 deletions

View File

@@ -0,0 +1,47 @@
using System.Collections.Generic;
namespace SabreTools.Models.Internal
{
/// <summary>
/// Specialized dictionary base for item types
/// </summary>
public abstract class DictionaryBase : Dictionary<string, object?>
{
/// <summary>
/// Read a key as the specified type, returning null on error
/// </summary>
public T? Read<T>(string key)
{
if (string.IsNullOrWhiteSpace(key))
return default;
if (!ContainsKey(key))
return default;
return (T?)this[key];
}
/// <summary>
/// Read a key as a bool, returning null on error
/// </summary>
public bool? ReadBool(string key) => Read<bool>(key);
/// <summary>
/// Read a key as a double, returning null on error
/// </summary>
public double? ReadDouble(string key) => Read<double>(key);
/// <summary>
/// Read a key as a long, returning null on error
/// </summary>
public long? ReadLong(string key) => Read<long>(key);
/// <summary>
/// Read a key as a string, returning null on error
/// </summary>
public string? ReadString(string key) => Read<string>(key);
/// <summary>
/// Read a key as a string[], returning null on error
/// </summary>
public string[]? ReadStringArray(string key) => Read<string[]>(key);
}
}