标签:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml.Linq; 6 7 namespace Tools 8 { 9 /// <summary> 10 /// Dictionary扩展方法 11 /// </summary> 12 public static class DictionaryExtensions 13 { 14 #region // Dictionary转XML 15 /// <summary> 16 /// 将字典转换成XML格式字符串 17 /// </summary> 18 /// <param name="dict">字典</param> 19 /// <returns>字符串</returns> 20 private static string GetXml<TKey, TValue>(Dictionary<TKey, TValue> dict) 21 { 22 StringBuilder xml = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?><xml>"); 23 foreach (var pair in dict) 24 { 25 xml.AppendFormat("<{0}><![CDATA[{1}]]></{2}>", pair.Key, pair.Value, pair.Key); 26 } 27 xml.Append("</xml>"); 28 return xml.ToString(); 29 } 30 31 /// <summary> 32 /// 将字典转换成XML格式字符串 33 /// </summary> 34 /// <param name="dict">字典</param> 35 /// <returns>字符串</returns> 36 public static string ToXml<TKey, TValue>(this Dictionary<TKey, TValue> dict) 37 where TKey : struct 38 where TValue : struct 39 { 40 return GetXml(dict); 41 } 42 43 /// <summary> 44 /// 将字典转换成XML格式字符串 45 /// </summary> 46 /// <param name="dict">字典</param> 47 /// <returns>字符串</returns> 48 public static string ToXml<TKey, TValue>(this Dictionary<string, TValue> dict) 49 where TValue : struct 50 { 51 return GetXml(dict); 52 } 53 54 /// <summary> 55 /// 将字典转换成XML格式字符串 56 /// </summary> 57 /// <param name="dict">字典</param> 58 /// <returns>字符串</returns> 59 public static string ToXml<TKey, TValue>(this Dictionary<TKey, string> dict) 60 where TKey : struct 61 { 62 return GetXml(dict); 63 } 64 65 /// <summary> 66 /// 将字典转换成XML格式字符串 67 /// </summary> 68 /// <param name="dict">字典</param> 69 /// <returns>字符串</returns> 70 public static string ToXml(this Dictionary<string, string> dict) 71 { 72 return GetXml(dict); 73 } 74 #endregion 75 76 #region // Dictionary转Json 77 /// <summary> 78 /// 将字典转换成Json字符串 79 /// </summary> 80 /// <typeparam name="TKey"></typeparam> 81 /// <typeparam name="TValue"></typeparam> 82 /// <param name="dict">字典</param> 83 /// <returns>字符串</returns> 84 private static string GetJson<TKey, TValue>(Dictionary<TKey, TValue> dict) 85 { 86 List<string> list = new List<string>(dict.Count); 87 foreach (var pair in dict) 88 { 89 list.Add(string.Format("\"{0}\":\"{1}\"", pair.Key, pair.Value)); 90 } 91 return "{" + string.Join(",", list.ToArray()) + "}"; 92 } 93 94 /// <summary> 95 /// 将字典转换成Json字符串 96 /// </summary> 97 /// <typeparam name="TKey"></typeparam> 98 /// <typeparam name="TValue"></typeparam> 99 /// <param name="dict">字典</param> 100 /// <returns>字符串</returns> 101 public static string ToJson<TKey, TValue>(this Dictionary<TKey, TValue> dict) 102 where TKey: struct 103 where TValue: struct 104 { 105 return GetJson(dict); 106 } 107 108 /// <summary> 109 /// 将字典转换成Json字符串 110 /// </summary> 111 /// <typeparam name="TKey"></typeparam> 112 /// <typeparam name="TValue"></typeparam> 113 /// <param name="dict">字典</param> 114 /// <returns>字符串</returns> 115 public static string ToJson<TKey, TValue>(this Dictionary<string, TValue> dict) 116 where TValue : struct 117 { 118 return GetJson(dict); 119 } 120 121 /// <summary> 122 /// 将字典转换成Json字符串 123 /// </summary> 124 /// <typeparam name="TKey"></typeparam> 125 /// <typeparam name="TValue"></typeparam> 126 /// <param name="dict">字典</param> 127 /// <returns>字符串</returns> 128 public static string ToJson<TKey, TValue>(this Dictionary<TKey, string> dict) 129 where TKey : struct 130 { 131 return GetJson(dict); 132 } 133 134 /// <summary> 135 /// 将字典转换成Json字符串 136 /// </summary> 137 /// <typeparam name="TKey"></typeparam> 138 /// <typeparam name="TValue"></typeparam> 139 /// <param name="dict">字典</param> 140 /// <returns>字符串</returns> 141 public static string ToJson(this Dictionary<string, string> dict) 142 { 143 return GetJson(dict); 144 } 145 #endregion 146 147 #region // Dictionary转Url键值对字符串 148 /// <summary> 149 /// 将字典的全部键值对拼接成Url键值对格式 150 /// </summary> 151 /// <typeparam name="TKey"></typeparam> 152 /// <typeparam name="TValue"></typeparam> 153 /// <param name="dict"></param> 154 /// <returns></returns> 155 public static string GetUrlKeyValuePair<TKey, TValue>(Dictionary<TKey, TValue> dict) 156 { 157 List<string> list = new List<string>(dict.Count); 158 foreach (var pair in dict) 159 { 160 list.Add(string.Format("{0}={1}", pair.Key, pair.Value)); 161 } 162 return string.Join("&", list.ToArray()); 163 } 164 165 /// <summary> 166 /// 将字典的全部键值对拼接成Url键值对格式 167 /// </summary> 168 /// <typeparam name="TKey"></typeparam> 169 /// <typeparam name="TValue"></typeparam> 170 /// <param name="dict"></param> 171 /// <returns></returns> 172 public static string ToUrlKeyValuePair<TKey, TValue>(this Dictionary<TKey, TValue> dict) 173 where TKey : struct 174 where TValue : struct 175 { 176 return GetUrlKeyValuePair(dict); 177 } 178 179 /// <summary> 180 /// 将字典的全部键值对拼接成Url键值对格式 181 /// </summary> 182 /// <typeparam name="TKey"></typeparam> 183 /// <typeparam name="TValue"></typeparam> 184 /// <param name="dict"></param> 185 /// <returns></returns> 186 public static string ToUrlKeyValuePair<TKey, TValue>(this Dictionary<string, TValue> dict) 187 where TValue : struct 188 { 189 return GetUrlKeyValuePair(dict); 190 } 191 192 /// <summary> 193 /// 将字典的全部键值对拼接成Url键值对格式 194 /// </summary> 195 /// <typeparam name="TKey"></typeparam> 196 /// <typeparam name="TValue"></typeparam> 197 /// <param name="dict"></param> 198 /// <returns></returns> 199 public static string ToUrlKeyValuePair<TKey, TValue>(this Dictionary<TKey, string> dict) 200 where TKey : struct 201 { 202 return GetUrlKeyValuePair(dict); 203 } 204 205 /// <summary> 206 /// 将字典的全部键值对拼接成Url键值对格式 207 /// </summary> 208 /// <typeparam name="TKey"></typeparam> 209 /// <typeparam name="TValue"></typeparam> 210 /// <param name="dict"></param> 211 /// <returns></returns> 212 public static string ToUrlKeyValuePair(this Dictionary<string, string> dict) 213 { 214 return GetUrlKeyValuePair(dict); 215 } 216 #endregion 217 218 #region // Dictionaty排序 219 220 #region // 升序 221 private static Dictionary<TKey, TValue> GetAsc<TKey, TValue>(Dictionary<TKey, TValue> dict) 222 { 223 return dict.OrderBy(e => e.Key).ToDictionary(e => e.Key, e => e.Value); 224 } 225 226 /// <summary> 227 /// 根据Key按升序排序 228 /// </summary> 229 /// <typeparam name="TKey"></typeparam> 230 /// <typeparam name="TValue"></typeparam> 231 /// <param name="dict"></param> 232 /// <returns></returns> 233 public static Dictionary<TKey, TValue> ToAsc<TKey, TValue>(this Dictionary<TKey, TValue> dict) 234 where TKey : struct 235 where TValue : struct 236 { 237 return GetAsc(dict); 238 } 239 240 /// <summary> 241 /// 根据Key按升序排序 242 /// </summary> 243 /// <typeparam name="TKey"></typeparam> 244 /// <typeparam name="TValue"></typeparam> 245 /// <param name="dict"></param> 246 /// <returns></returns> 247 public static Dictionary<string, TValue> ToAsc<TKey, TValue>(this Dictionary<string, TValue> dict) 248 where TValue : struct 249 { 250 return GetAsc(dict); 251 } 252 253 /// <summary> 254 /// 根据Key按升序排序 255 /// </summary> 256 /// <typeparam name="TKey"></typeparam> 257 /// <typeparam name="TValue"></typeparam> 258 /// <param name="dict"></param> 259 /// <returns></returns> 260 public static Dictionary<TKey, string> ToAsc<TKey, TValue>(this Dictionary<TKey, string> dict) 261 where TKey : struct 262 { 263 return GetAsc(dict); 264 } 265 266 /// <summary> 267 /// 根据Key按升序排序 268 /// </summary> 269 /// <typeparam name="TKey"></typeparam> 270 /// <typeparam name="TValue"></typeparam> 271 /// <param name="dict"></param> 272 /// <returns></returns> 273 public static Dictionary<string, string> ToAsc(this Dictionary<string, string> dict) 274 { 275 return GetAsc(dict); 276 } 277 #endregion 278 279 #region // 降序 280 private static Dictionary<TKey, TValue> GetDesc<TKey, TValue>(Dictionary<TKey, TValue> dict) 281 { 282 return dict.OrderByDescending(e => e.Key).ToDictionary(e => e.Key, e => e.Value); 283 } 284 285 /// <summary> 286 /// 根据Key按降序排序 287 /// </summary> 288 /// <typeparam name="TKey"></typeparam> 289 /// <typeparam name="TValue"></typeparam> 290 /// <param name="dict"></param> 291 /// <returns></returns> 292 public static Dictionary<TKey, TValue> ToDesc<TKey, TValue>(this Dictionary<TKey, TValue> dict) 293 where TKey : struct 294 where TValue : struct 295 { 296 return GetDesc(dict); 297 } 298 299 /// <summary> 300 /// 根据Key按降序排序 301 /// </summary> 302 /// <typeparam name="TKey"></typeparam> 303 /// <typeparam name="TValue"></typeparam> 304 /// <param name="dict"></param> 305 /// <returns></returns> 306 public static Dictionary<string, TValue> ToDesc<TKey, TValue>(this Dictionary<string, TValue> dict) 307 where TValue : struct 308 { 309 return GetDesc(dict); 310 } 311 312 /// <summary> 313 /// 根据Key按降序排序 314 /// </summary> 315 /// <typeparam name="TKey"></typeparam> 316 /// <typeparam name="TValue"></typeparam> 317 /// <param name="dict"></param> 318 /// <returns></returns> 319 public static Dictionary<TKey, string> ToDesc<TKey, TValue>(this Dictionary<TKey, string> dict) 320 where TKey : struct 321 { 322 return GetDesc(dict); 323 } 324 325 /// <summary> 326 /// 根据Key按降序排序 327 /// </summary> 328 /// <typeparam name="TKey"></typeparam> 329 /// <typeparam name="TValue"></typeparam> 330 /// <param name="dict"></param> 331 /// <returns></returns> 332 public static Dictionary<string, string> ToDesc(this Dictionary<string, string> dict) 333 { 334 return GetDesc(dict); 335 } 336 #endregion 337 338 #endregion 339 340 #region // Dictionary操作 341 /// <summary> 342 /// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常 343 /// </summary> 344 public static Dictionary<TKey, TValue> TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value) 345 { 346 if (dict.ContainsKey(key) == false) 347 { 348 dict.Add(key, value); 349 } 350 return dict; 351 } 352 353 /// <summary> 354 /// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换 355 /// </summary> 356 public static Dictionary<TKey, TValue> AddOrReplace<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value) 357 { 358 dict[key] = value; 359 return dict; 360 } 361 362 /// <summary> 363 /// 向字典中批量添加键值对 364 /// </summary> 365 /// <param name="replaceExisted">如果已存在,是否替换</param> 366 public static Dictionary<TKey, TValue> AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> values, bool replaceExisted) 367 { 368 foreach (var item in values) 369 { 370 if (dict.ContainsKey(item.Key) == false || replaceExisted) 371 { 372 dict[item.Key] = item.Value; 373 } 374 } 375 return dict; 376 } 377 378 /// <summary> 379 /// 获取与指定的键相关联的值,如果没有则返回输入的默认值 380 /// </summary> 381 //public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue)) // .Net 3.5不支持此写法 382 public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue) 383 { 384 return dict.ContainsKey(key) ? dict[key] : defaultValue; 385 } 386 #endregion 387 388 #region // Dictionary辅助方法 389 /// <summary> 390 /// 判断字典是否有元素 391 /// </summary> 392 /// <typeparam name="T"></typeparam> 393 /// <param name="list"></param> 394 /// <returns>布尔值,True表示有元素,False表示没有元素</returns> 395 public static bool HasItem<TKey, TValue>(this Dictionary<TKey, TValue> dict) 396 { 397 return dict != null && dict.Count > 0; 398 } 399 400 /// <summary> 401 /// 判断字典是否为空 402 /// </summary> 403 /// <typeparam name="TKey"></typeparam> 404 /// <typeparam name="TValue"></typeparam> 405 /// <param name="dict"></param> 406 /// <returns></returns> 407 public static bool IsNullOrEmpty<TKey, TValue>(this Dictionary<TKey, TValue> dict) 408 { 409 return dict == null || dict.Count == 0; 410 } 411 #endregion 412 } 413 414 /// <summary> 415 /// List扩展方法 416 /// </summary> 417 public static class ListExtensions 418 { 419 /// <summary> 420 /// 判断集合是否有元素 421 /// </summary> 422 /// <typeparam name="T"></typeparam> 423 /// <param name="list"></param> 424 /// <returns>布尔值,True表示有元素,False表示没有元素</returns> 425 public static bool HasItem<T>(this List<T> list) 426 { 427 return list != null && list.Count > 0; 428 } 429 430 /// <summary> 431 /// 判断集合是否为空 432 /// </summary> 433 /// <typeparam name="T"></typeparam> 434 /// <param name="list"></param> 435 /// <returns></returns> 436 public static bool IsNullOrEmpty<T>(this List<T> list) 437 { 438 return list == null || list.Count == 0; 439 } 440 } 441 442 /// <summary> 443 /// String扩展方法 444 /// </summary> 445 public static class StringExtensions 446 { 447 /// <summary> 448 /// 判断当前字符串是否为null或者为Empty 449 /// </summary> 450 /// <param name="value"></param> 451 /// <returns></returns> 452 public static bool IsNullOrEmpty(this string value) 453 { 454 //return string.IsNullOrEmpty(value); 455 456 return value == null || value == string.Empty; 457 } 458 459 /// <summary> 460 /// 判断当前字符串是否为null、Empty或者空白 461 /// </summary> 462 /// <param name="value"></param> 463 /// <returns></returns> 464 public static bool IsNullOrWhiteSpace(this string value) 465 { 466 //if (value != null) 467 //{ 468 // return string.IsNullOrEmpty(value.Trim()); 469 //} 470 //return true; 471 472 return value == null || value == string.Empty || value.Trim() == string.Empty; 473 } 474 } 475 476 /// <summary> 477 /// XElement扩展方法 478 /// </summary> 479 public static class XElementExtensions 480 { 481 /// <summary> 482 /// 根据节点名称获取节点值,获取失败时返回null 483 /// </summary> 484 /// <param name="doc"></param> 485 /// <param name="nodeName">节点名称</param> 486 /// <returns>节点值</returns> 487 public static string TryGetValue(this XElement doc, string nodeName) 488 { 489 XElement xElement = doc.Element(nodeName); 490 if (xElement != null) 491 { 492 return xElement.Value; 493 } 494 return null; 495 } 496 } 497 }
自己写的一些数据类型扩展方法,还不是很完善,只是现在用到什么就写了什么而已,以后会慢慢更新
标签:
原文地址:http://www.cnblogs.com/Jarvan-Chan/p/5422127.html