• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#Dictionary扩展方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

主要用于接口请求,数据转换

 #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;
        }
       #endregion

 

 

 

        public static void SetKey<T>(this Dictionary<string, T> dic, string key, T value)
        {
            if (dic.ContainsKey(key))
            {
                dic[key] = value;
            }
            else
            {
                dic.Add(key, value);
            }
        }

        public static T GetKey<T>(this Dictionary<string, T> dic, string key, T defaultValue= default(T))
        {
            if (dic.ContainsKey(key))
            {
                return dic[key];
            }
            else
            {
                return defaultValue;
            }
        }

 原文:https://www.cnblogs.com/zisai/p/11050729.html 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中的函数(一)无参无返回值的函数发布时间:2022-07-10
下一篇:
C#lambda递归发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap