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

转载于:https://www.cnblogs.com/zisai/p/11050729.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值