127 lines
3.2 KiB
C#
127 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
|
|
namespace CadParamPluging.Common
|
|
{
|
|
[Serializable]
|
|
public class ParamBag
|
|
{
|
|
public List<ParamValue> Items { get; set; }
|
|
|
|
public ParamBag()
|
|
{
|
|
Items = new List<ParamValue>();
|
|
}
|
|
|
|
public void Set(string key, string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Items == null)
|
|
{
|
|
Items = new List<ParamValue>();
|
|
}
|
|
|
|
var existing = Items.FirstOrDefault(x => string.Equals(x.Key, key, StringComparison.OrdinalIgnoreCase));
|
|
if (existing == null)
|
|
{
|
|
Items.Add(new ParamValue { Key = key.Trim(), Value = value });
|
|
}
|
|
else
|
|
{
|
|
existing.Value = value;
|
|
}
|
|
}
|
|
|
|
public string GetString(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key) || Items == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return Items.FirstOrDefault(x => string.Equals(x.Key, key.Trim(), StringComparison.OrdinalIgnoreCase))?.Value;
|
|
}
|
|
|
|
public double GetDouble(string key, double fallback = 0)
|
|
{
|
|
var s = GetString(key);
|
|
if (string.IsNullOrWhiteSpace(s))
|
|
{
|
|
return fallback;
|
|
}
|
|
|
|
if (double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var v))
|
|
{
|
|
return v;
|
|
}
|
|
|
|
if (double.TryParse(s, NumberStyles.Float, CultureInfo.CurrentCulture, out v))
|
|
{
|
|
return v;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
public int GetInt(string key, int fallback = 0)
|
|
{
|
|
var s = GetString(key);
|
|
if (string.IsNullOrWhiteSpace(s))
|
|
{
|
|
return fallback;
|
|
}
|
|
|
|
if (int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var v))
|
|
{
|
|
return v;
|
|
}
|
|
|
|
if (int.TryParse(s, NumberStyles.Integer, CultureInfo.CurrentCulture, out v))
|
|
{
|
|
return v;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
|
|
public bool GetBool(string key, bool fallback = false)
|
|
{
|
|
var s = GetString(key);
|
|
if (string.IsNullOrWhiteSpace(s))
|
|
{
|
|
return fallback;
|
|
}
|
|
|
|
if (bool.TryParse(s, out var v))
|
|
{
|
|
return v;
|
|
}
|
|
|
|
if (string.Equals(s, "1", StringComparison.OrdinalIgnoreCase) || string.Equals(s, "是", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (string.Equals(s, "0", StringComparison.OrdinalIgnoreCase) || string.Equals(s, "否", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ParamValue
|
|
{
|
|
public string Key { get; set; }
|
|
public string Value { get; set; }
|
|
}
|
|
}
|