Set的时候把double类型转为String类型之后再Set。
Get的时候转为类即可。不丢失精度。
return new StackRedisHelper().HashGetAllDynamic(RedisCustomKey.TCPS_LotRuleConfig + lotNum).ParseDictionaryToModel<LotRuleConfigDto>().ToJsonString();
public Dictionary<string, dynamic> HashGetAllDynamic(string key)
{
key = AddSysCustomKey(key);
Dictionary<string, dynamic> result = new Dictionary<string, dynamic>();
return Do(db =>
{
var value = db.HashGetAll(key);
foreach (var m in value)
{
result.Add(m.Name, m.Value);
}
return result;
});
}
/// <summary>
/// 把Model转换为DataRow
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="mod"></param>
/// <returns></returns>
public static T ParseDictionaryToModel<T>(this Dictionary<string, dynamic> dict)
{
//
T obj =
default(T);
obj = Activator.CreateInstance<T>();
//根据Key值设定 Columns
foreach (KeyValuePair<string, dynamic> item in dict)
{
PropertyInfo prop = obj.GetType().GetProperty(item.Key);
if (prop != null)
{
if (!string.IsNullOrEmpty(item.Value))
{
object value = item.Value;
//Nullable 获取Model类字段的真实类型
Type itemType = Nullable.GetUnderlyingType(prop.PropertyType) == null ? prop.PropertyType : Nullable.GetUnderlyingType(prop.PropertyType);
//根据Model类字段的真实类型进行转换
prop.SetValue(obj, Convert.ChangeType(value, itemType), null);
}
}
}
return obj;
}
单独Get 某个 double类型Field的时候也不会报错。
return new StackRedisHelper().HashGet<double>(RedisCustomKey.TCPS_LotRuleConfig + lotNum, "LotValue");
/// <summary>
/// 从hash表获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public T HashGet<T>(string key, string dataKey)
{
key = AddSysCustomKey(key);
return Do(db =>
{
var value = db.HashGet(key, dataKey);
if (value.IsNullOrEmpty)
return default(T);
return ConvertObj<T>(value);
});
}