zoukankan      html  css  js  c++  java
  • 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;
            }
           #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 

  • 相关阅读:
    201215-03-19---cocos2dx内存管理--具体解释
    sqlHelper的增删改查
    Java Web的数据库操作(一)
    Myeclipse 中添加mysql的jdbc驱动
    MySQL索引的创建、删除和查看
    搭建Windows下Java Web开发环境
    Qt 格式化字符串
    实现C++模板类头文件和实现文件分离的方法
    Qt Creator实现状态栏显示
    Win7 64位下配置Qt5.3和Wincap
  • 原文地址:https://www.cnblogs.com/zisai/p/11050729.html
Copyright © 2011-2022 走看看