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

 #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. 在不同的系统中的virtualbox中安装Ubuntu SDK

    对非常多的开发人员来说.你们可能使用的不是Ubuntu操作系统.在这样的情况下,开发人员须要在自己的操作系统中(OS X及Windows)安装virtualbox,并在VirtualBox中安装Ubu ...

  2. c# DataGridView样式设置无效

    对DataGridView中的某些行设置样式时,无效,最后发现,我是先设置完样式再进行展现的this.controls.Add,应该先展现完了,再设置样式.

  3. POJ 2629:Common permutation

    Common permutation Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5510   Accepted: 168 ...

  4. 第九周 Leetcode 502. IPO (HARD)

    Leetcode 502 一个公司 目前有资产W 可以选择实现K个项目,每个项目要求公司当前有一定的资产,且每个项目可以使公司的总资产增加一个非负数. 项目数50000 设计一个优先队列,对于当前状态 ...

  5. 如何精通javascript

    http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...

  6. 为了一个句号,写了好多行的代码——值!(html 表单的处理)

    个人信息表 <span style="font-size:18px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML ...

  7. ffmpeg 有用命令 (转载)

    转自:http://blog.csdn.net/simongyley/article/details/9984167 1.将h264文件解码为yuv文件 ffmpeg -i file.h264 fil ...

  8. bzoj 1774: [Usaco2009 Dec]Toll 过路费【排序+Floyd】

    非常迷的一道题啊 我觉得挺对的版本只得了30 总之就是Floyd·改,开两个数组,一个是d[i][j]就是普通的只有边权的最短路,a[i][j]是题目要求的那种 具体改的地方是把枚举中转点的地方把中转 ...

  9. Syntax error on token ";", , expected 错误

    eclipse错误提示如图: 错误代码如图: 一开始百思不得其解,后来终于发现问题的原因所在,java中变量的声明可以不在方法中,但语句只能出现在方法中,可以再声明变量的时候就赋初值,但如果要单独赋值 ...

  10. HDU4947GCD Array(莫比乌斯反演+树状数组)

    题面 传送门 题解 orz ljz 相当于每一个数要加上 \[v\times [\gcd(i,n)=d]=v\times [\gcd(i/d,n/d)=1]=v\times \sum_{p|{i\ov ...