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

 #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. Android基础新手教程——4.1.1 Activity初学乍练

    Android基础新手教程--4.1.1 Activity初学乍练 标签(空格分隔): Android基础新手教程 本节引言: 本节開始解说Android的四大组件之中的一个的Activity(活动) ...

  2. 科普:google的数字图书馆

    https://books.google.com/ngrams Google Ngram Viewer,她利用google所拥有的所有图书作为资源,为你提供单词和短语历年使用次数的展示图标.数据化了数 ...

  3. Python开发【1.4数据类型】

    1.数字 数字数据类型用于存储数值. 他们是不可改变的数据类型,这意味着改变数字数据类型会分配一个新的对象. # 创建对象 var1 = 1 var2 = 2 # 删除对象 del var1 del ...

  4. iOS开发个人开发账号的证书详细使用及介绍

    本人也和大家一样在学习iOS的开发,在开发当中最烦的就是证书出问题,主要是没有理解透证书的含义,因此查阅了一些资料,才对证书有了一定的认识,本文章就是介绍个人的个人理解,有不对的地方大加可以留言提醒, ...

  5. [翻译]NUnit---Exception && Utility Methods (六)

    网址:http://www.cnblogs.com/kim01/archive/2013/04/01/2994378.html Exception Asserts (NUnit 2.5) Assert ...

  6. brew安装PHP7 swoole

    环境: 系统:mac os High Sierra 10.13.3 php版本:7.0.27_19 1.安装homebrew brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在M ...

  7. 昆石VOS3000_2.1.2.0完整安装包及安装脚本

    安装包下载地址 http://www.51voip.org/post/57.html 安装教程: 上传安装包 ·给整个目录授权 chmod 777 /root/vosintsall 1.安装前准备 首 ...

  8. 基于CentOS7.5的 Rsync 服务详解

    第1章 Rsync概述 1.1 Rsync基本概述 rsync是一款开源的备份工具,可以在不同服务器(主机)之间进行同步备份, 可实现完全备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用 ...

  9. Linux C编程之二:Linux基础

    1.Linux的特点 (1)Linux就是一个操作系统(作为用户和计算机之间接口的软件程序) 注:操作系统的功能:命令解释,进程管理,内存管理,输入输出(I/O)操作和外围设备管理,文件管理 (2)特 ...

  10. SQLServer2005 维护计划 无法删除

    1.查看"维护计划"对象的ID use msdbselect * from sysmaintplan_plansselect * from sysmaintplan_logsele ...