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

 #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 = )
{
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 = )
{
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 = )
{
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 = )
{
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 = )
{
return (float)dic.getDouble(key, isNullDefault, defaultValue);
} public static float getFloat(this Dictionary<string, object> dic, string key, bool isNullDefault = true, double defaultValue = )
{
return (float)dic.getDouble(key, isNullDefault, defaultValue);
} public static int getInt32(this Dictionary<string, object> dic, string key, bool isNullDefault = true, int defaultValue = )
{
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 = )
{
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 == "";
} 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 == "";
} 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

c# Dictionary 扩展方法的更多相关文章

  1. c# 扩展方法奇思妙用基础篇五:Dictionary<TKey, TValue> 扩展

    Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多 ...

  2. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

  3. C#的扩展方法解析

    在使用面向对象的语言进行项目开发的过程中,较多的会使用到“继承”的特性,但是并非所有的场景都适合使用“继承”特性,在设计模式的一些基本原则中也有较多的提到. 继承的有关特性的使用所带来的问题:对象的继 ...

  4. DataTable扩展方法ToList<T>()、ToJSON()、ToArrayList()

    /// <summary> /// 扩展方法类 /// </summary> public static class CommonExtension { /// <sum ...

  5. 【开源】OSharp框架解说系列(3):扩展方法

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  6. WebAPi添加常用扩展方法及思维发散

    前言 在WebAPi中我们通常需要得到请求信息中的查询字符串或者请求头中数据再或者是Cookie中的数据,如果需要大量获取,此时我们应该想到封装一个扩展类来添加扩展方法,从而实现简便快捷的获取. We ...

  7. 再谈扩展方法,从string.IsNullOrEmpty()说起

    string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...

  8. c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)

    转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...

  9. 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)

    假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的 ...

随机推荐

  1. Gif验证码类

    package com.paic.bics.common.utils.vcode; import java.awt.AlphaComposite; import java.awt.Color; imp ...

  2. 稀疏表达是要求信号在该模型下的sparse code,只有少数的non-zero elements

    为什么sparse representation比起其它成分分析方法(DFT,Wavelet)能得到更好的效果? - 知乎  https://www.zhihu.com/question/241241 ...

  3. 怎么样关掉红米note开发者选项

    进 系统设置\应用 ,找到“设置”点进去,清一下数据,再打开“设置”查看,就没有“开发者选项”了

  4. 学习 Shell —— 认识 shell

    0. 认识 shell shell 是一个命令行解释器(interpreter),它会输出一个提示符,等待输入一个命令,然后执行该命令.如果该命令行的第一个单词不是一个内置的 shell 命令,那么 ...

  5. java用户角色权限设计

    实现业务系统中的用户权限管理 B/S系统中的权限比C/S中的更显的重要,C/S系统因为具有特殊的客户端,所以访问用户的权限检测可以通过客户端实现或通过客户端+服务器检测实现,而B/S中,浏览器是每一台 ...

  6. Java多线程系列二——Thread类的方法

    Thread实现Runnable接口并实现了大量实用的方法 public static native void yield(); 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机 ...

  7. 2017西安网络赛B_Coin

    样例输入 2 2 1 1 3 1 2 样例输出 500000004 555555560 思路: n重伯努利实验概率分布题. 设q=1-p,p为事件概率. Y为出现偶数次的概率. 所以  Y=1/2*( ...

  8. TCP/IP与Http与socket的关系

    1 理清概念: TCP/IP是一个大的协议族(只不过TCP和IP是super star所以就这么命名了),它包括了: 应用层协议:FTP.HTTP.TELNET.SMTP.DNS(协议): 传输层协议 ...

  9. 6.12---esultMap查询所有

  10. redis之有序集合类型(Zset)——排行榜的实现

    当数据库对排序支持的不是很好,可以利用redis有序集合排序 原文链接:http://blog.csdn.net/loophome/article/details/50373202