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

 #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. scikit-learn:3. Model selection and evaluation

    參考:http://scikit-learn.org/stable/model_selection.html 有待翻译,敬请期待: 3.1. Cross-validation: evaluating ...

  2. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  3. mac svn cornerstone 破解版资源以及使用方法(仅供学习,非商业使用)

    mac svn 可视化客户端,找了好久,不知道是我搜索的有问题还是怎么了,没有特别好用的. 后来发现了一个大神做的破解版的 cornerstone,具体大神的博客我给忘记了,后续找到会贴出地址,以供膜 ...

  4. 【codevs1306】广播操的游戏

    求字符串内的非空子串的数量 后缀数组!!! #include<algorithm> #include<cstdlib> #include<cstring> #inc ...

  5. Message: unknown error: Element is not clickable at point

    Message: unknown error: Element is not clickable at point google chrome - Debugging "Element is ...

  6. HTTP要点概述:五,HTTP的无状态性,持久连接,Cookie

    一,HTTP的无状态性: HTTP 是一种不保存状态,无状态(stateless)协议.HTTP 协议自身不对请求和响应之间的通信状态进行保存.也就是说在 HTTP 这个级别,协议对于发送过的请求或响 ...

  7. GitHub上README.md教程(copy)

    [说明:转载于http://blog.csdn.net/kaitiren/article/details/38513715] 最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编 ...

  8. eclipse maven创建web项目

    记录地址 jdk设置及文件包miss 实例下载地址 创建SSM整合项目 一.使用Eclipse中的maven插件创建web项目 1: 2: 3: 4: 5:maven web项目创建成功.(去掉ind ...

  9. sql2000数据库置疑造成的原因以及如何解决置疑

    造成数据库置疑一般有以下几点: 1)电脑非法关机或者意外停电: 2)磁盘有坏道或者损坏: 3)数据库感染病毒,日志文件损坏: 4)非正常情况下移动数据库文件 5)系统,硬盘,经常强制性关机(如断电)类 ...

  10. v-contextmenu的使用(右键菜单)

    先来个自己改写的图: 代码: 结构:<div class="wrap" v-contextmenu:contextmenu> <v-contextmenu ref ...