/Main/src/DynamicDataDisplay/Common/Auxiliary/IListExtensions.cs

# · C# · 52 lines · 47 code · 5 blank · 0 comment · 0 complexity · bc5db742b0de3ae9988a2b96a3d39744 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Microsoft.Research.DynamicDataDisplay
  6. {
  7. public static class IListExtensions
  8. {
  9. public static void AddMany<T>(this IList<T> collection, IEnumerable<T> addingItems)
  10. {
  11. foreach (var item in addingItems)
  12. {
  13. collection.Add(item);
  14. }
  15. }
  16. public static void AddMany<T>(this IList<T> collection, params T[] children)
  17. {
  18. foreach (var child in children)
  19. {
  20. collection.Add(child);
  21. }
  22. }
  23. public static void RemoveAllOfType<T>(this IList<T> collection, Type type)
  24. {
  25. var children = collection.Where(el => type.IsAssignableFrom(el.GetType())).ToArray();
  26. foreach (var child in children)
  27. {
  28. collection.Remove((T)child);
  29. }
  30. }
  31. public static void RemoveAll<T, TDelete>(this IList<T> collection)
  32. {
  33. var children = collection.OfType<TDelete>().ToArray();
  34. foreach (var child in children)
  35. {
  36. collection.Remove((T)(object)child);
  37. }
  38. }
  39. public static void RemoveAll<T>(this IList<T> collection, params T[] elements)
  40. {
  41. foreach (var item in elements)
  42. {
  43. collection.Remove(item);
  44. }
  45. }
  46. }
  47. }