MissingMethods.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Text;
  5. namespace RolePlayingGameData
  6. {
  7. /// <summary>
  8. /// Extends NET framework classes with the methods missing on XBox
  9. /// Implements only basic functionality of those methods
  10. /// </summary>
  11. public static class MissingMethods
  12. {
  13. /// <summary>
  14. /// Implements List.RemoveAll(Predicate match) method
  15. /// </summary>
  16. public static int RemoveAll<T>(this List<T> that, Predicate<T> match)
  17. {
  18. int count = that.Count;
  19. List<T> res = new List<T>(that.Count);
  20. foreach (var item in that)
  21. {
  22. if (match(item) == false)
  23. {
  24. res.Add(item);
  25. }
  26. }
  27. that.Clear();
  28. that.AddRange(res);
  29. return that.Count - count;
  30. }
  31. /// <summary>
  32. /// Implements List.Find(Predicate match) method
  33. /// </summary>
  34. public static T Find<T>(this List<T> that, Predicate<T> match)
  35. {
  36. foreach (var item in that)
  37. {
  38. if (match(item) == true)
  39. {
  40. return item;
  41. }
  42. }
  43. return default(T);
  44. }
  45. /// <summary>
  46. /// Implements List.Exists(Predicate match) method
  47. /// </summary>
  48. public static bool Exists<T>(this List<T> that, Predicate<T> match)
  49. {
  50. foreach (var item in that)
  51. {
  52. if (match(item) == true)
  53. {
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. /// <summary>
  60. /// Implements List.TrueForAll(Predicate match) method
  61. /// </summary>
  62. public static bool TrueForAll<T>(this List<T> that, Predicate<T> match)
  63. {
  64. foreach (var item in that)
  65. {
  66. if (match(item) == false)
  67. {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. /// <summary>
  74. /// Implements List.FindIndex(Predicate match) method
  75. /// </summary>
  76. public static int FindIndex<T>(this List<T> that, Predicate<T> match)
  77. {
  78. for (int i = 0; i < that.Count; i++)
  79. {
  80. if (match(that[i]) == true)
  81. {
  82. return i;
  83. }
  84. }
  85. return -1;
  86. }
  87. /// <summary>
  88. /// Implements XmlReader.ReadElementString(string name) method
  89. /// </summary>
  90. public static string ReadElementString(this XmlReader that, string name)
  91. {
  92. return that.ReadElementContentAsString();
  93. }
  94. }
  95. }