c# Dictionary 扩展方法
Posted zisai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# Dictionary 扩展方法相关的知识,希望对你有一定的参考价值。
主要用于接口请求,数据转换
#region Dictionary 扩展方法 public static string getString(this Dictionary<string, string> dic, string key, bool isNullDefault = true, string Default = "") if (dic != null && dic.ContainsKey(key)) return dic[key]; else if (isNullDefault) return Default; else throw new Exception($"数据‘key‘丢失!!"); public static object getValue(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string Default = "") if (dic != null && dic.ContainsKey(key)) return dic[key]; else if (isNullDefault) return Default; else throw new Exception($"数据‘key‘丢失!!"); public static string getString(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string defaultValue = "") if (dic != null && dic.ContainsKey(key)) object obj = dic[key]; if (obj == null) return ""; return Convert.ToString(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static decimal getDecimal(this Dictionary<string, object> dic, string key, bool isNullDefault = true, decimal defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDecimal(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static decimal getDecimal(this Dictionary<string, string> dic, string key, bool isNullDefault = true, decimal defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDecimal(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static double getDouble(this Dictionary<string, object> dic, string key, bool isNullDefault = true, double defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDouble(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static double getDouble(this Dictionary<string, string> dic, string key, bool isNullDefault = true, double defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDouble(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static float getFloat(this Dictionary<string, string> dic, string key, bool isNullDefault = true, double defaultValue = 0) return (float)dic.getDouble(key, isNullDefault, defaultValue); public static float getFloat(this Dictionary<string, object> dic, string key, bool isNullDefault = true, double defaultValue = 0) return (float)dic.getDouble(key, isNullDefault, defaultValue); public static int getInt32(this Dictionary<string, object> dic, string key, bool isNullDefault = true, int defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToInt32(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static int getInt32(this Dictionary<string, string> dic, string key, bool isNullDefault = true, int defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToInt32(dic[key] ?? "" + defaultValue); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static bool getBoolean(this Dictionary<string, string> dic, string key, bool isNullDefault = true, string defaultValue = "false") string value = dic.getString(key, isNullDefault, defaultValue); return value.ToLower() == "true" || value == "1"; public static bool getBoolean(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string defaultValue = "false") string value = dic.getString(key, isNullDefault, defaultValue); return value.ToLower() == "true" || value == "1"; public static T ChangeType<T>(this Dictionary<string, object> dic, string key, bool isNullDefault = true) where T : class if (!dic.ContainsKey(key)) return null; object value = dic.getValue(key); T result = JsonHelper.DeserializeJsonToObject<T>(value == null ? null : value.ToString()); return result; /// <summary> /// 添加员工ID /// </summary> /// <param name="dic"></param> public static void AddEmpId(this Dictionary<string, string> dic) if (dic != null) dic.Add("empId", GlobalClass.LoginEmp.Id.ToString()); /// <summary> /// 添加终端ID /// </summary> /// <param name="dic"></param> public static void AddTermId(this Dictionary<string, string> dic) if (dic != null) dic.Add("termId", GlobalClass.TermID.ToString()); /// <summary> /// 添加门点ID /// </summary> /// <param name="dic"></param> public static void AddShopId(this Dictionary<string, string> dic) if (dic != null) dic.Add("shopId", GlobalClass.LoginEmp.ShopId.ToString()); #endregion
#region Dictionary 扩展方法
public static string getString(this Dictionary<string, string> dic, string key, bool isNullDefault = true, string Default = "") if (dic != null && dic.ContainsKey(key)) return dic[key]; else if (isNullDefault) return Default; else throw new Exception($"数据‘key‘丢失!!"); public static object getValue(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string Default = "") if (dic != null && dic.ContainsKey(key)) return dic[key]; else if (isNullDefault) return Default; else throw new Exception($"数据‘key‘丢失!!"); public static string getString(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string defaultValue = "") if (dic != null && dic.ContainsKey(key)) object obj = dic[key]; if (obj == null) return ""; return Convert.ToString(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!");
public static decimal getDecimal(this Dictionary<string, object> dic, string key, bool isNullDefault = true, decimal defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDecimal(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static decimal getDecimal(this Dictionary<string, string> dic, string key, bool isNullDefault = true, decimal defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDecimal(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static double getDouble(this Dictionary<string, object> dic, string key, bool isNullDefault = true, double defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDouble(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static double getDouble(this Dictionary<string, string> dic, string key, bool isNullDefault = true, double defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToDouble(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!");
public static float getFloat(this Dictionary<string, string> dic, string key, bool isNullDefault = true, double defaultValue = 0) return (float)dic.getDouble(key, isNullDefault, defaultValue);
public static float getFloat(this Dictionary<string, object> dic, string key, bool isNullDefault = true, double defaultValue = 0) return (float)dic.getDouble(key, isNullDefault, defaultValue);
public static int getInt32(this Dictionary<string, object> dic, string key, bool isNullDefault = true, int defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToInt32(dic[key]); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!"); public static int getInt32(this Dictionary<string, string> dic, string key, bool isNullDefault = true, int defaultValue = 0) if (dic != null && dic.ContainsKey(key)) return Convert.ToInt32(dic[key] ?? "" + defaultValue); else if (isNullDefault) return defaultValue; else throw new Exception($"数据‘key‘丢失!!");
public static bool getBoolean(this Dictionary<string, string> dic, string key, bool isNullDefault = true, string defaultValue = "false") string value = dic.getString(key, isNullDefault, defaultValue); return value.ToLower() == "true" || value == "1";
public static bool getBoolean(this Dictionary<string, object> dic, string key, bool isNullDefault = true, string defaultValue = "false") string value = dic.getString(key, isNullDefault, defaultValue); return value.ToLower() == "true" || value == "1";
public static T ChangeType<T>(this Dictionary<string, object> dic, string key, bool isNullDefault = true) where T : class if (!dic.ContainsKey(key)) return null; object value = dic.getValue(key); T result = JsonHelper.DeserializeJsonToObject<T>(value == null ? null : value.ToString()); return result; /// <summary> /// 添加员工ID /// </summary> /// <param name="dic"></param> public static void AddEmpId(this Dictionary<string, string> dic) if (dic != null) dic.Add("empId", GlobalClass.LoginEmp.Id.ToString()); /// <summary> /// 添加终端ID /// </summary> /// <param name="dic"></param> public static void AddTermId(this Dictionary<string, string> dic) if (dic != null) dic.Add("termId", GlobalClass.TermID.ToString()); /// <summary> /// 添加门点ID /// </summary> /// <param name="dic"></param> public static void AddShopId(this Dictionary<string, string> dic) if (dic != null) dic.Add("shopId", GlobalClass.LoginEmp.ShopId.ToString()); #endregion
以上是关于c# Dictionary 扩展方法的主要内容,如果未能解决你的问题,请参考以下文章