175 lines
4.5 KiB
C#
175 lines
4.5 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查参数是否存在且有有效值
|
|
/// </summary>
|
|
public bool Has(string key)
|
|
{
|
|
var s = GetString(key);
|
|
return !string.IsNullOrWhiteSpace(s);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取可空的 double 值,参数不存在或无效时返回 null
|
|
/// </summary>
|
|
public double? GetDoubleOrNull(string key)
|
|
{
|
|
var s = GetString(key);
|
|
if (string.IsNullOrWhiteSpace(s))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
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 null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有已设置的参数Key列表
|
|
/// </summary>
|
|
public IEnumerable<string> GetKeys()
|
|
{
|
|
if (Items == null)
|
|
{
|
|
return Enumerable.Empty<string>();
|
|
}
|
|
|
|
return Items
|
|
.Where(x => x != null && !string.IsNullOrWhiteSpace(x.Key))
|
|
.Select(x => x.Key);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ParamValue
|
|
{
|
|
public string Key { get; set; }
|
|
public string Value { get; set; }
|
|
}
|
|
}
|