CollectionBase.cs 3.7 KB

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