CollectionBase<T> Class
As you know, CollectionBase in the namespace System.Collections is the base class to help you create your own strong typed collection class. The following CollectionBase<T> class is the generic version of the CollectionBase class.
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Canaware.ApplicationFramework.Core.Collections
{
public class CollectionBase<T> :
IList<T>, IList,
ICollection<T>, ICollection,
IEnumerable<T>, IEnumerable
{
private List<T> _list;
//private bool sorted = true;
public CollectionBase()
{
}
public CollectionBase(int capacity)
{
_list = new List<T>(capacity);
}
[ComVisible(false)]
public int Capacity
{
get
{
if (this._list == null)
{
this._list = new List<T>();
}
return this._list.Capacity;
}
set
{
if (this._list == null)
{
this._list = new List<T>();
}
this._list.Capacity = value;
}
}
protected List<T> InnerList
{
get
{
if (this._list == null)
{
this._list = new List<T>();
}
return this._list;
}
}
public void Clear()
{
this.OnClear();
this.InnerList.Clear();
this.OnClearComplete();
}
public void RemoveAt(int index)
{
T value = InnerList[index];
OnValidate(value);
OnRemove(index, value);
InnerList.RemoveAt(index);
OnRemoveComplete(index, value);
}
public int Count
{
get
{
return InnerList.Count;
}
}
public bool IsReadOnly
{
get
{
return ((ICollection<T>)InnerList).IsReadOnly;
}
}
#region Notification Events
protected virtual void OnClear()
{
}
protected virtual void OnClearComplete()
{
}
protected virtual void OnInsert(int index, T value)
{
}
protected virtual void OnInsertComplete(int index, T value)
{
}
protected virtual void OnRemove(int index, T value)
{
}
protected virtual void OnRemoveComplete(int index, T value)
{
}
protected virtual void OnSet(int index, T oldValue, T value)
{
}
protected virtual void OnSetComplete(int index, T oldValue, T value)
{
}
protected virtual void OnValidate(T value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
}
#endregion
#region IList<T> Members
int IList<T>.IndexOf(T item)
{
return InnerList.IndexOf(item);
}
void IList<T>.Insert(int index, T item)
{
OnValidate(item);
OnInsert(index, item);
InnerList.Insert(index, item);
try
{
OnInsertComplete(index, item);
}
catch
{
InnerList.RemoveAt(index);
throw;
}
}
T IList<T>.this[int index]
{
get
{
return InnerList[index];
}
set
{
if (index < 0 || index >= InnerList.Count)
{
throw new ArgumentOutOfRangeException("index");
}
OnValidate(value);
T oldValue = InnerList[index];
OnSet(index, oldValue, value);
InnerList[index] = value;
try
{
OnSetComplete(index, oldValue, value);
}
catch
{
InnerList[index] = oldValue;
throw;
}
}
}
#endregion
#region ICollection<T> Members
void ICollection<T>.Add(T item)
{
OnValidate(item);
int index = InnerList.Count;
OnInsert(index, item);
InnerList.Add(item);
try
{
OnInsertComplete(index, item);
}
catch
{
InnerList.RemoveAt(index);
throw;
}
}
bool ICollection<T>.Contains(T item)
{
return InnerList.Contains(item);
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
InnerList.CopyTo(array, arrayIndex);
}
bool ICollection<T>.Remove(T item)
{
OnValidate(item);
int index = InnerList.IndexOf(item);
if (index < 0) return false;
OnRemove(index, item);
InnerList.Remove(item);
OnRemoveComplete(index, item);
return true;
}
#endregion
#region IEnumerable<T> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return InnerList.GetEnumerator();
}
#endregion
#region IList Members
int IList.Add(object value)
{
OnValidate((T)value);
int index = InnerList.Count;
OnInsert(index, (T)value);
index = ((IList)InnerList).Add(value);
try
{
OnInsertComplete(index, (T)value);
}
catch
{
InnerList.RemoveAt(index);
throw;
}
return index;
}
bool IList.Contains(object value)
{
return ((IList)InnerList).Contains(value);
}
int IList.IndexOf(object value)
{
return ((IList)InnerList).IndexOf(value);
}
void IList.Insert(int index, object value)
{
OnValidate((T)value);
OnInsert(index, (T)value);
((IList)InnerList).Insert(index, value);
try
{
OnInsertComplete(index, (T)value);
}
catch
{
InnerList.RemoveAt(index);
throw;
}
}
bool IList.IsFixedSize
{
get
{
return ((IList)InnerList).IsFixedSize;
}
}
void IList.Remove(object value)
{
OnValidate((T)value);
int index = InnerList.IndexOf((T)value);
if (index < 0)
{
throw new ArgumentException("The element cannot be found.", "value");
}
OnRemove(index, (T)value);
((IList)InnerList).Remove(value);
OnRemoveComplete(index, (T)value);
}
object IList.this[int index]
{
get
{
return InnerList[index];
}
set
{
if (index < 0 || index >= InnerList.Count)
{
throw new ArgumentOutOfRangeException("index");
}
OnValidate((T)value);
T oldValue = InnerList[index];
OnSet(index, oldValue, (T)value);
InnerList[index] = (T)value;
try
{
OnSetComplete(index, oldValue, (T)value);
}
catch
{
InnerList[index] = oldValue;
throw;
}
}
}
#endregion
#region ICollection Members
void ICollection.CopyTo(Array array, int index)
{
((ICollection)InnerList).CopyTo(array, index);
}
bool ICollection.IsSynchronized
{
get
{
return ((ICollection)InnerList).IsSynchronized;
}
}
object ICollection.SyncRoot
{
get
{
return ((ICollection)InnerList).SyncRoot;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)InnerList).GetEnumerator();
}
#endregion
}
}