/// <summary> /// 从指定的配置接口中获取参数值。 /// </summary> /// <param name="settings">配置接口</param> /// <param name="key">参数名称</param> /// <param name="maxValue">允许的最大数值</param> /// <param name="minValue">允许的最小数值</param> /// <param name="defaultValue">参数值不符合条件时返回的默认值。</param> /// <returns>指定参数名称对应的参数值,如果参数值不符合要求,则返回指定的默认值。</returns> public static Decimal GetDecimal(IDictionary <string, string> settings, string key, Decimal minValue = Decimal.MinValue, Decimal maxValue = Decimal.MaxValue, Decimal defaultValue = 0) { var text = GetString(settings, key); try { var result = SettingsParser.ParseDecimal(text); if (result >= minValue && result <= maxValue) { return(result); } else { return(defaultValue); } } catch (Exception /*ex*/) { return(defaultValue); } }
/// <summary> /// 从指定的配置接口中获取字节数组。 /// </summary> public static byte[] GetByteArray(IDictionary <string, string> settings, string key) { var text = GetString(settings, key); var result = new List <byte>(); var splitedText = text.Trim(). Split(new string[] { ";", ";", ",", ",", " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (var item in splitedText) { try { var value = SettingsParser.ParseDecimal(item); result.Add(Convert.ToByte(value)); } catch (FormatException /*ex*/) { throw new Exception(string.Format("无法将字符串'{0}'转化为数值。", item)); } } return(result.ToArray()); }