using System; using System.Collections.Generic; using System.Linq; namespace Masuit.Tools.Files; /// /// 布尔类型的特殊处理选项 /// public class BoolOptions { private Dictionary _boolStringLookup; private string _trueString = "true"; private string _falseString = "false"; /// /// 非0值作为true /// public bool NonZeroNumbersAreTrue { get; set; } /// /// 构造函数 /// /// public BoolOptions(StringComparer comparer = null) { _boolStringLookup = new Dictionary(comparer ?? StringComparer.CurrentCultureIgnoreCase) { [_trueString] = true, [_falseString] = false, ["yes"] = true, ["no"] = false, ["on"] = true, ["off"] = false, ["1"] = true, ["0"] = false, ["y"] = true, ["n"] = false, }; NonZeroNumbersAreTrue = true; } /// /// 设置布尔值对应的词语 /// /// /// /// public void SetBoolWords(IEnumerable words) { if (words == null) { throw new ArgumentNullException(nameof(words)); } var word = words.FirstOrDefault(w => w.Value); if (word == null) { throw new InvalidOperationException("布尔词列表不包含“true”值的条目。"); } _trueString = word.Word; word = words.FirstOrDefault(w => w.Value == false); if (word == null) { throw new InvalidOperationException("布尔词列表不包含“false”值的条目。"); } _falseString = word.Word; _boolStringLookup = words.ToDictionary(w => w.Word, w => w.Value); } internal string ToString(bool value) => value ? _trueString : _falseString; internal bool TryParse(string s, out bool value) { if (s != null) { if (_boolStringLookup.TryGetValue(s, out bool b)) { value = b; return true; } if (NonZeroNumbersAreTrue && int.TryParse(s, out int i)) { value = i != 0; return true; } } value = false; return false; } }