1 /// <summary>
2 /// The i enumerable extension.
3 /// </summary>
4 public static class IEnumerableExtension
5 {
6 #region Join
7 /// <summary>
8 /// 根据字符串拆分数组
9 /// </summary>
10 /// <param name="source">
11 /// 要拆分的数组
12 /// </param>
13 /// <param name="separator">
14 /// 拆分符
15 /// </param>
16 /// <returns>
17 /// 字符串
18 /// </returns>
19 public static string Join(this IEnumerable<string> source, string separator)
20 {
21 if (source == null)
22 {
23 throw new ArgumentNullException("source");
24 }
25
26 if (separator == null)
27 {
28 throw new ArgumentNullException("separator");
29 }
30
31 return source.Aggregate((x, y) => x + separator + y);
32 }
33
34 /// <summary>
35 /// 根据字符串拆分数组
36 /// </summary>
37 /// <typeparam name="TSource">类型</typeparam>
38 /// <param name="soucre"> 要拆分的数组</param>
39 /// <param name="separator">拆分符</param>
40 /// <param name="map">拆分条件</param>
41 /// <returns>字符串 <see cref="string"/></returns>
42 public static string Join<TSource>(this IEnumerable<TSource> soucre, string separator, Func<TSource, string> map)
43 {
44 if (soucre == null)
45 {
46 throw new ArgumentNullException("source");
47 }
48
49 if (separator == null)
50 {
51 throw new ArgumentNullException("separator");
52 }
53
54 if (map == null)
55 {
56 throw new ArgumentNullException("map");
57 }
58
59 var enumerable = soucre as TSource[] ?? soucre.ToArray();
60 return Join(enumerable.Select(map), separator);
61 }
62 #endregion
63
64 #region Sort
65 /// <summary>
66 /// 多条件排序扩展方法
67 /// </summary>
68 /// <typeparam name="TSource">TSource</typeparam>
69 /// <param name="sources">sources</param>
70 /// <param name="keySelector">keySelector</param>
71 /// <returns>排序结果</returns>
72 public static IEnumerable<TSource> Sort<TSource>(this IEnumerable<TSource> sources, params KeyValuePair<bool, Func<TSource, object>>[] keySelector)
73 {
74 if (sources == null)
75 {
76 throw new ArgumentNullException("sources");
77 }
78
79 IOrderedEnumerable<TSource> orderBys = null;
80 int i = 0;
81 foreach (var func in keySelector)
82 {
83 if (i == 0)
84 {
85 orderBys = func.Key ? sources.OrderBy(func.Value) : sources.OrderByDescending(func.Value);
86 }
87 else
88 {
89 if (orderBys != null)
90 {
91 orderBys = func.Key ? orderBys.ThenBy(func.Value) : orderBys.ThenByDescending(func.Value);
92 }
93 }
94
95 i++;
96 }
97
98 return orderBys;
99 }
100 #endregion
101
102 #region MaxElement
103 /// <summary>
104 /// 获取最大值的当前对象
105 /// </summary>
106 /// <typeparam name="TElement">TElement</typeparam>
107 /// <typeparam name="TData">TData</typeparam>
108 /// <param name="source">source</param>
109 /// <param name="selector">selector</param>
110 /// <returns>MaxValue</returns>
111 public static TElement MaxElement<TElement, TData>(this IEnumerable<TElement> source, Func<TElement, TData> selector)
112 where TData : IComparable<TData>
113 {
114 return ComparableElement(source, selector, true);
115 }
116 #endregion
117
118 #region MinElement
119 /// <summary>
120 /// 获取最小值的当前对象
121 /// </summary>
122 /// <typeparam name="TElement">TElement</typeparam>
123 /// <typeparam name="TData">TData</typeparam>
124 /// <param name="source">source</param>
125 /// <param name="selector">selector</param>
126 /// <returns>MaxValue</returns>
127 public static TElement MinElement<TElement, TData>(this IEnumerable<TElement> source, Func<TElement, TData> selector)
128 where TData : IComparable<TData>
129 {
130 return ComparableElement(source, selector, false);
131 }
132 #endregion
133
134 #region Max
135 /// <summary>
136 /// 求最大值
137 /// </summary>
138 /// <typeparam name="TSource">TSource</typeparam>
139 /// <param name="source">source</param>
140 /// <param name="predicate">predicate</param>
141 /// <param name="selector">selector</param>
142 /// <returns>最大值</returns>
143 public static decimal Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal> selector)
144 {
145 if (source == null)
146 {
147 throw new ArgumentNullException("source");
148 }
149
150 return source.Where(predicate).Select(selector).Max();
151 }
152
153 /// <summary>
154 /// 求最大值
155 /// </summary>
156 /// <typeparam name="TSource">TSource</typeparam>
157 /// <param name="source">source</param>
158 /// <param name="predicate">predicate</param>
159 /// <param name="selector">selector</param>
160 /// <returns>最大值</returns>
161 public static double Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double> selector)
162 {
163 if (source == null)
164 {
165 throw new ArgumentNullException("source");
166 }
167
168 return source.Where(predicate).Select(selector).Max();
169 }
170
171 /// <summary>
172 /// 求最大值
173 /// </summary>
174 /// <typeparam name="TSource">TSource</typeparam>
175 /// <param name="source">source</param>
176 /// <param name="predicate">predicate</param>
177 /// <param name="selector">selector</param>
178 /// <returns>最大值</returns>
179 public static int Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int> selector)
180 {
181 if (source == null)
182 {
183 throw new ArgumentNullException("source");
184 }
185
186 return source.Where(predicate).Select(selector).Max();
187 }
188
189 /// <summary>
190 /// 求最大值
191 /// </summary>
192 /// <typeparam name="TSource">TSource</typeparam>
193 /// <param name="source">source</param>
194 /// <param name="predicate">predicate</param>
195 /// <param name="selector">selector</param>
196 /// <returns>最大值</returns>
197 public static long Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long> selector)
198 {
199 if (source == null)
200 {
201 throw new ArgumentNullException("source");
202 }
203
204 return source.Where(predicate).Select(selector).Max();
205 }
206
207 /// <summary>
208 /// 求最大值
209 /// </summary>
210 /// <typeparam name="TSource">TSource</typeparam>
211 /// <param name="source">source</param>
212 /// <param name="predicate">predicate</param>
213 /// <param name="selector">selector</param>
214 /// <returns>最大值</returns>
215 public static float Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float> selector)
216 {
217 if (source == null)
218 {
219 throw new ArgumentNullException("source");
220 }
221
222 return source.Where(predicate).Select(selector).Max();
223 }
224
225 /// <summary>
226 /// 求最大值
227 /// </summary>
228 /// <typeparam name="TSource">TSource</typeparam>
229 /// <param name="source">source</param>
230 /// <param name="predicate">predicate</param>
231 /// <param name="selector">selector</param>
232 /// <returns>最大值</returns>
233 public static decimal? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal?> selector)
234 {
235 if (source == null)
236 {
237 throw new ArgumentNullException("source");
238 }
239
240 return source.Where(predicate).Select(selector).Max();
241 }
242
243 /// <summary>
244 /// 求最大值
245 /// </summary>
246 /// <typeparam name="TSource">TSource</typeparam>
247 /// <param name="source">source</param>
248 /// <param name="predicate">predicate</param>
249 /// <param name="selector">selector</param>
250 /// <returns>最大值</returns>
251 public static double? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double?> selector)
252 {
253 if (source == null)
254 {
255 throw new ArgumentNullException("source");
256 }
257
258 return source.Where(predicate).Select(selector).Max();
259 }
260
261 /// <summary>
262 /// 求最大值
263 /// </summary>
264 /// <typeparam name="TSource">TSource</typeparam>
265 /// <param name="source">source</param>
266 /// <param name="predicate">predicate</param>
267 /// <param name="selector">selector</param>
268 /// <returns>最大值</returns>
269 public static int? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int?> selector)
270 {
271 if (source == null)
272 {
273 throw new ArgumentNullException("source");
274 }
275
276 return source.Where(predicate).Select(selector).Max();
277 }
278
279 /// <summary>
280 /// 求最大值
281 /// </summary>
282 /// <typeparam name="TSource">TSource</typeparam>
283 /// <param name="source">source</param>
284 /// <param name="predicate">predicate</param>
285 /// <param name="selector">selector</param>
286 /// <returns>最大值</returns>
287 public static long? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long?> selector)
288 {
289 if (source == null)
290 {
291 throw new ArgumentNullException("source");
292 }
293
294 return source.Where(predicate).Select(selector).Max();
295 }
296
297 /// <summary>
298 /// 求最大值
299 /// </summary>
300 /// <typeparam name="TSource">TSource</typeparam>
301 /// <param name="source">source</param>
302 /// <param name="predicate">predicate</param>
303 /// <param name="selector">selector</param>
304 /// <returns>最大值</returns>
305 public static float? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float?> selector)
306 {
307 if (source == null)
308 {
309 throw new ArgumentNullException("source");
310 }
311
312 return source.Where(predicate).Select(selector).Max();
313 }
314 #endregion
315
316 #region Min
317 /// <summary>
318 /// 求最小值
319 /// </summary>
320 /// <typeparam name="TSource">TSource</typeparam>
321 /// <param name="source">source</param>
322 /// <param name="predicate">predicate</param>
323 /// <param name="selector">selector</param>
324 /// <returns>最小值</returns>
325 public static decimal Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal> selector)
326 {
327 if (source == null)
328 {
329 throw new ArgumentNullException("source");
330 }
331
332 return source.Where(predicate).Select(selector).Min();
333 }
334
335 /// <summary>
336 /// 求最小值
337 /// </summary>
338 /// <typeparam name="TSource">TSource</typeparam>
339 /// <param name="source">source</param>
340 /// <param name="predicate">predicate</param>
341 /// <param name="selector">selector</param>
342 /// <returns>最小值</returns>
343 public static double Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double> selector)
344 {
345 if (source == null)
346 {
347 throw new ArgumentNullException("source");
348 }
349
350 return source.Where(predicate).Select(selector).Min();
351 }
352
353 /// <summary>
354 /// 求最小值
355 /// </summary>
356 /// <typeparam name="TSource">TSource</typeparam>
357 /// <param name="source">source</param>
358 /// <param name="predicate">predicate</param>
359 /// <param name="selector">selector</param>
360 /// <returns>最小值</returns>
361 public static int Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int> selector)
362 {
363 if (source == null)
364 {
365 throw new ArgumentNullException("source");
366 }
367
368 return source.Where(predicate).Select(selector).Min();
369 }
370
371 /// <summary>
372 /// 求最小值
373 /// </summary>
374 /// <typeparam name="TSource">TSource</typeparam>
375 /// <param name="source">source</param>
376 /// <param name="predicate">predicate</param>
377 /// <param name="selector">selector</param>
378 /// <returns>最小值</returns>
379 public static long Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long> selector)
380 {
381 if (source == null)
382 {
383 throw new ArgumentNullException("source");
384 }
385
386 return source.Where(predicate).Select(selector).Min();
387 }
388
389 /// <summary>
390 /// 求最小值
391 /// </summary>
392 /// <typeparam name="TSource">TSource</typeparam>
393 /// <param name="source">source</param>
394 /// <param name="predicate">predicate</param>
395 /// <param name="selector">selector</param>
396 /// <returns>最小值</returns>
397 public static float Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float> selector)
398 {
399 if (source == null)
400 {
401 throw new ArgumentNullException("source");
402 }
403
404 return source.Where(predicate).Select(selector).Min();
405 }
406
407 /// <summary>
408 /// 求最小值
409 /// </summary>
410 /// <typeparam name="TSource">TSource</typeparam>
411 /// <param name="source">source</param>
412 /// <param name="predicate">predicate</param>
413 /// <param name="selector">selector</param>
414 /// <returns>最小值</returns>
415 public static decimal? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal?> selector)
416 {
417 if (source == null)
418 {
419 throw new ArgumentNullException("source");
420 }
421
422 return source.Where(predicate).Select(selector).Min();
423 }
424
425 /// <summary>
426 /// 求最小值
427 /// </summary>
428 /// <typeparam name="TSource">TSource</typeparam>
429 /// <param name="source">source</param>
430 /// <param name="predicate">predicate</param>
431 /// <param name="selector">selector</param>
432 /// <returns>最小值</returns>
433 public static double? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double?> selector)
434 {
435 if (source == null)
436 {
437 throw new ArgumentNullException("source");
438 }
439
440 return source.Where(predicate).Select(selector).Min();
441 }
442
443 /// <summary>
444 /// 求最小值
445 /// </summary>
446 /// <typeparam name="TSource">TSource</typeparam>
447 /// <param name="source">source</param>
448 /// <param name="predicate">predicate</param>
449 /// <param name="selector">selector</param>
450 /// <returns>最小值</returns>
451 public static int? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int?> selector)
452 {
453 if (source == null)
454 {
455 throw new ArgumentNullException("source");
456 }
457
458 return source.Where(predicate).Select(selector).Min();
459 }
460
461 /// <summary>
462 /// 求最小值
463 /// </summary>
464 /// <typeparam name="TSource">TSource</typeparam>
465 /// <param name="source">source</param>
466 /// <param name="predicate">predicate</param>
467 /// <param name="selector">selector</param>
468 /// <returns>最小值</returns>
469 public static long? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long?> selector)
470 {
471 if (source == null)
472 {
473 throw new ArgumentNullException("source");
474 }
475
476 return source.Where(predicate).Select(selector).Min();
477 }
478
479 /// <summary>
480 /// 求最小值
481 /// </summary>
482 /// <typeparam name="TSource">TSource</typeparam>
483 /// <param name="source">source</param>
484 /// <param name="predicate">predicate</param>
485 /// <param name="selector">selector</param>
486 /// <returns>最小值</returns>
487 public static float? Min<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float?> selector)
488 {
489 if (source == null)
490 {
491 throw new ArgumentNullException("source");
492 }
493
494 return source.Where(predicate).Select(selector).Min();
495 }
496 #endregion
497
498 #region Sum
499 /// <summary>
500 /// 求和
501 /// </summary>
502 /// <typeparam name="TSource">TSource</typeparam>
503 /// <param name="source">source</param>
504 /// <param name="predicate">predicate</param>
505 /// <param name="selector">selector</param>
506 /// <returns>和</returns>
507 public static decimal Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal> selector)
508 {
509 if (source == null)
510 {
511 throw new ArgumentNullException("source");
512 }
513
514 return source.Where(predicate).Select(selector).Sum();
515 }
516
517 /// <summary>
518 /// 求和
519 /// </summary>
520 /// <typeparam name="TSource">TSource</typeparam>
521 /// <param name="source">source</param>
522 /// <param name="predicate">predicate</param>
523 /// <param name="selector">selector</param>
524 /// <returns>和</returns>
525 public static double Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double> selector)
526 {
527 if (source == null)
528 {
529 throw new ArgumentNullException("source");
530 }
531
532 return source.Where(predicate).Select(selector).Sum();
533 }
534
535 /// <summary>
536 /// 求和
537 /// </summary>
538 /// <typeparam name="TSource">TSource</typeparam>
539 /// <param name="source">source</param>
540 /// <param name="predicate">predicate</param>
541 /// <param name="selector">selector</param>
542 /// <returns>和</returns>
543 public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int> selector)
544 {
545 if (source == null)
546 {
547 throw new ArgumentNullException("source");
548 }
549
550 return source.Where(predicate).Select(selector).Sum();
551 }
552
553 /// <summary>
554 /// 求和
555 /// </summary>
556 /// <typeparam name="TSource">TSource</typeparam>
557 /// <param name="source">source</param>
558 /// <param name="predicate">predicate</param>
559 /// <param name="selector">selector</param>
560 /// <returns>和</returns>
561 public static long Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long> selector)
562 {
563 if (source == null)
564 {
565 throw new ArgumentNullException("source");
566 }
567
568 return source.Where(predicate).Select(selector).Sum();
569 }
570
571 /// <summary>
572 /// 求和
573 /// </summary>
574 /// <typeparam name="TSource">TSource</typeparam>
575 /// <param name="source">source</param>
576 /// <param name="predicate">predicate</param>
577 /// <param name="selector">selector</param>
578 /// <returns>和</returns>
579 public static float Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float> selector)
580 {
581 if (source == null)
582 {
583 throw new ArgumentNullException("source");
584 }
585
586 return source.Where(predicate).Select(selector).Sum();
587 }
588
589 /// <summary>
590 /// 求和
591 /// </summary>
592 /// <typeparam name="TSource">TSource</typeparam>
593 /// <param name="source">source</param>
594 /// <param name="predicate">predicate</param>
595 /// <param name="selector">selector</param>
596 /// <returns>和</returns>
597 public static decimal? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, decimal?> selector)
598 {
599 if (source == null)
600 {
601 throw new ArgumentNullException("source");
602 }
603
604 return source.Where(predicate).Select(selector).Sum(result => result.GetValueOrDefault());
605 }
606
607 /// <summary>
608 /// 求和
609 /// </summary>
610 /// <typeparam name="TSource">TSource</typeparam>
611 /// <param name="source">source</param>
612 /// <param name="predicate">predicate</param>
613 /// <param name="selector">selector</param>
614 /// <returns>和</returns>
615 public static double? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, double?> selector)
616 {
617 if (source == null)
618 {
619 throw new ArgumentNullException("source");
620 }
621
622 return source.Where(predicate).Select(selector).Sum();
623 }
624
625 /// <summary>
626 /// 求和
627 /// </summary>
628 /// <typeparam name="TSource">TSource</typeparam>
629 /// <param name="source">source</param>
630 /// <param name="predicate">predicate</param>
631 /// <param name="selector">selector</param>
632 /// <returns>和</returns>
633 public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, int?> selector)
634 {
635 if (source == null)
636 {
637 throw new ArgumentNullException("source");
638 }
639
640 return source.Where(predicate).Select(selector).Sum();
641 }
642
643 /// <summary>
644 /// 求和
645 /// </summary>
646 /// <typeparam name="TSource">TSource</typeparam>
647 /// <param name="source">source</param>
648 /// <param name="predicate">predicate</param>
649 /// <param name="selector">selector</param>
650 /// <returns>和</returns>
651 public static long? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, long?> selector)
652 {
653 if (source == null)
654 {
655 throw new ArgumentNullException("source");
656 }
657
658 return source.Where(predicate).Select(selector).Sum();
659 }
660
661 /// <summary>
662 /// 求和
663 /// </summary>
664 /// <typeparam name="TSource">TSource</typeparam>
665 /// <param name="source">source</param>
666 /// <param name="predicate">predicate</param>
667 /// <param name="selector">selector</param>
668 /// <returns>和</returns>
669 public static float? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, float?> selector)
670 {
671 if (source == null)
672 {
673 throw new ArgumentNullException("source");
674 }
675
676 return source.Where(predicate).Select(selector).Sum();
677 }
678
679 #endregion
680
681 #region private
682 /// <summary>
683 /// 获取最大or最小值的当前对象
684 /// </summary>
685 /// <typeparam name="TElement">TElement</typeparam>
686 /// <typeparam name="TData">TData</typeparam>
687 /// <param name="source">source</param>
688 /// <param name="selector">selector</param>
689 /// <param name="isMax">最大还是最小</param>
690 /// <returns>MaxValue</returns>
691 private static TElement ComparableElement<TElement, TData>(IEnumerable<TElement> source, Func<TElement, TData> selector, bool isMax)
692 where TData : IComparable<TData>
693 {
694 if (source == null)
695 {
696 throw new ArgumentNullException("source");
697 }
698
699 if (selector == null)
700 {
701 throw new ArgumentNullException("selector");
702 }
703
704 bool firstElement = true;
705 TElement result = default(TElement);
706 TData maxValue = default(TData);
707 foreach (TElement element in source)
708 {
709 var candidate = selector(element);
710 if (!firstElement)
711 {
712 if (isMax && candidate.CompareTo(maxValue) <= 0)
713 {
714 continue;
715 }
716
717 if (!isMax && candidate.CompareTo(maxValue) > 0)
718 {
719 continue;
720 }
721 }
722
723 firstElement = false;
724 maxValue = candidate;
725 result = element;
726 }
727
728 return result;
729 }
730 #endregion
731
732 #region 拆分字符串
733 /// <summary>
734 /// 根据字符串拆分字符串
735 /// </summary>
736 /// <param name="source">要拆分的字符串</param>
737 /// <param name="separator">拆分符</param>
738 /// <returns>数组</returns>
739 public static string[] Split(this string source, string separator)
740 {
741 if (source == null)
742 {
743 throw new ArgumentNullException("source");
744 }
745
746 if (separator == null)
747 {
748 throw new ArgumentNullException("separator");
749 }
750
751 string[] strtmp = new string[1];
752 // ReSharper disable once StringIndexOfIsCultureSpecific.2
753 int index = source.IndexOf(separator, 0);
754 if (index < 0)
755 {
756 strtmp[0] = source;
757 return strtmp;
758 }
759
760 strtmp[0] = source.Substring(0, index);
761 return Split(source.Substring(index + separator.Length), separator, strtmp);
762 }
763
764 /// <summary>
765 /// 采用递归将字符串分割成数组
766 /// </summary>
767 /// <param name="source">要拆分的字符串</param>
768 /// <param name="separator">拆分符</param>
769 /// <param name="attachArray">attachArray</param>
770 /// <returns>string[]</returns>
771 private static string[] Split(string source, string separator, string[] attachArray)
772 {
773 // while循环的方式
774 while (true)
775 {
776 string[] strtmp = new string[attachArray.Length + 1];
777 attachArray.CopyTo(strtmp, 0);
778
779 // ReSharper disable once StringIndexOfIsCultureSpecific.2
780 int index = source.IndexOf(separator, 0);
781 if (index < 0)
782 {
783 strtmp[attachArray.Length] = source;
784 return strtmp;
785 }
786
787 strtmp[attachArray.Length] = source.Substring(0, index);
788 source = source.Substring(index + separator.Length);
789 attachArray = strtmp;
790 }
791
792 // 递归的方式
793 /*
794 string[] strtmp = new string[attachArray.Length + 1];
795 attachArray.CopyTo(strtmp, 0);
796
797 // ReSharper disable once StringIndexOfIsCultureSpecific.2
798 int index = source.IndexOf(separator, 0);
799 if (index < 0)
800 {
801 strtmp[attachArray.Length] = source;
802 return strtmp;
803 }
804 else
805 {
806 strtmp[attachArray.Length] = source.Substring(0, index);
807 return Split(source.Substring(index + separator.Length), separator, strtmp);
808 }*/
809 }
810
811 #endregion
812
813 /// <summary>
814 /// JavaScript方式序列化json
815 /// </summary>
816 /// <param name="obj">输出对象</param>
817 /// <returns>JSON字符串</returns>
818 public static string Serialization(this object obj)
819 {
820 JavaScriptSerializer json = new JavaScriptSerializer();
821 return json.Serialize(obj);
822 }
823
824 /// <summary>
825 /// 获取Json的Model(反序列化)
826 /// </summary>
827 /// <typeparam name="T">对象类型</typeparam>
828 /// <param name="json">json字符串</param>
829 /// <returns>T对象</returns>
830 public static T Deserializ<T>(this string json)
831 {
832 T t = default(T);
833 try
834 {
835 JavaScriptSerializer serializer = new JavaScriptSerializer();
836 return serializer.Deserialize<T>(json);
837 }
838 catch (Exception)
839 {
840 return t;
841 }
842 }
843
844 /// <summary>
845 /// XML序列化方式深复制
846 /// </summary>
847 /// <typeparam name="T">类型</typeparam>
848 /// <param name="obj">复制对象</param>
849 /// <returns>结果</returns>
850 public static T DeepCopy<T>(this T obj)
851 {
852 object retval;
853 using (MemoryStream ms = new MemoryStream())
854 {
855 XmlSerializer xml = new XmlSerializer(typeof(T));
856 xml.Serialize(ms, obj);
857 ms.Seek(0, SeekOrigin.Begin);
858 retval = xml.Deserialize(ms);
859 ms.Close();
860 }
861
862 return (T)retval;
863 }
864 }