c# Dictionary 扩展方法
2021-05-30 07:23
标签:type dict span 扩展方法 def static ejs dem int 主要用于接口请求,数据转换 #region Dictionary 扩展方法 c# Dictionary 扩展方法 标签:type dict span 扩展方法 def static ejs dem int 原文地址:https://www.cnblogs.com/zisai/p/11050729.html #region Dictionary 扩展方法
public static string getString(this Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, string> dic, string key, bool isNullDefault = true, double defaultValue = 0)
{
return (float)dic.getDouble(key, isNullDefault, defaultValue);
}
public static float getFloat(this Dictionarystring, object> dic, string key, bool isNullDefault = true, double defaultValue = 0)
{
return (float)dic.getDouble(key, isNullDefault, defaultValue);
}
public static int getInt32(this Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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
public static string getString(this Dictionary
public static decimal getDecimal(this Dictionary
public static float getFloat(this Dictionary
public static float getFloat(this Dictionary
public static int getInt32(this Dictionary
public static bool getBoolean(this Dictionary
public static bool getBoolean(this Dictionary
public static T ChangeType
下一篇:wpf 自定义Button按钮
文章标题:c# Dictionary 扩展方法
文章链接:http://soscw.com/index.php/essay/89462.html