CollectionBase.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Collections.CollectionBase.cs
  3. //
  4. // Author:
  5. // Nick Drochak II ([email protected])
  6. //
  7. // (C) 2001 Nick Drochak II
  8. //
  9. using System;
  10. namespace System.Collections {
  11. [Serializable]
  12. public abstract class CollectionBase : IList, ICollection, IEnumerable {
  13. // private instance properties
  14. private ArrayList myList;
  15. // public instance properties
  16. public int Count { get { return InnerList.Count; } }
  17. // Public Instance Methods
  18. public IEnumerator GetEnumerator() { return InnerList.GetEnumerator(); }
  19. public void Clear() {
  20. OnClear();
  21. InnerList.Clear();
  22. OnClearComplete();
  23. }
  24. public void RemoveAt (int index) {
  25. object objectToRemove;
  26. objectToRemove = InnerList[index];
  27. OnValidate(objectToRemove);
  28. OnRemove(index, objectToRemove);
  29. InnerList.RemoveAt(index);
  30. OnRemoveComplete(index, objectToRemove);
  31. }
  32. // Protected Instance Constructors
  33. protected CollectionBase() {
  34. this.myList = new ArrayList();
  35. }
  36. // Protected Instance Properties
  37. protected ArrayList InnerList {get { return this.myList; } }
  38. protected IList List {get { return this; } }
  39. // Protected Instance Methods
  40. protected virtual void OnClear() { }
  41. protected virtual void OnClearComplete() { }
  42. protected virtual void OnInsert(int index, object value) { }
  43. protected virtual void OnInsertComplete(int index, object value) { }
  44. protected virtual void OnRemove(int index, object value) { }
  45. protected virtual void OnRemoveComplete(int index, object value) { }
  46. protected virtual void OnSet(int index, object oldValue, object newValue) { }
  47. protected virtual void OnSetComplete(int index, object oldValue, object newValue) { }
  48. protected virtual void OnValidate(object value) {
  49. if (null == value) {
  50. throw new System.ArgumentNullException("CollectionBase.OnValidate: Invalid parameter value passed to method: null");
  51. }
  52. }
  53. // ICollection methods
  54. void ICollection.CopyTo(Array array, int index) {
  55. InnerList.CopyTo(array, index);
  56. }
  57. object ICollection.SyncRoot {
  58. get { return InnerList.SyncRoot; }
  59. }
  60. bool ICollection.IsSynchronized {
  61. get { return InnerList.IsSynchronized; }
  62. }
  63. // IList methods
  64. int IList.Add (object value) {
  65. int newPosition;
  66. OnValidate(value);
  67. newPosition = InnerList.Count;
  68. OnInsert(newPosition, value);
  69. InnerList.Add(value);
  70. OnInsertComplete(newPosition, value);
  71. return newPosition;
  72. }
  73. bool IList.Contains (object value) {
  74. return InnerList.Contains(value);
  75. }
  76. int IList.IndexOf (object value) {
  77. return InnerList.IndexOf(value);
  78. }
  79. void IList.Insert (int index, object value) {
  80. OnValidate(value);
  81. OnInsert(index, value);
  82. InnerList.Insert(index, value);
  83. OnInsertComplete(index, value);
  84. }
  85. void IList.Remove (object value) {
  86. int removeIndex;
  87. OnValidate(value);
  88. removeIndex = InnerList.IndexOf(value);
  89. OnRemove(removeIndex, value);
  90. InnerList.Remove(value);
  91. OnRemoveComplete(removeIndex, value);
  92. }
  93. // IList properties
  94. bool IList.IsFixedSize {
  95. get { return InnerList.IsFixedSize; }
  96. }
  97. bool IList.IsReadOnly {
  98. get { return InnerList.IsReadOnly; }
  99. }
  100. object IList.this[int index] {
  101. get { return InnerList[index]; }
  102. set {
  103. object oldValue;
  104. // make sure we have been given a valid value
  105. OnValidate(value);
  106. // save a reference to the object that is in the list now
  107. oldValue = InnerList[index];
  108. OnSet(index, oldValue, value);
  109. InnerList[index] = value;
  110. OnSetComplete(index, oldValue, value);
  111. }
  112. }
  113. }
  114. }