99 lines
2.9 KiB
C#
99 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace JD.Collections.ObjectModel
|
|
{
|
|
/// <summary>
|
|
/// ReadOnlyListBase is a base class that can be used to more easily implement the
|
|
/// generic IList<T> interfaces for a read-only collection:
|
|
/// a collection that does not allow adding or removing elements.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>To use ReadOnlyListBase as a base class, the derived class must override
|
|
/// the Count, GetEnumerator and GetItem methods. </para>
|
|
/// <para>ICollection<T>.Contains and IList<T>.IndexOf need not be implemented by the
|
|
/// derived class, but it should be strongly considered, because the ReadOnlyListBase implementation
|
|
/// may not be very efficient.</para>
|
|
/// </remarks>
|
|
/// <typeparam name="T">The item type of the collection.</typeparam>
|
|
[Serializable]
|
|
public abstract class ReadOnlyListBase<T> : ReadOnlyCollectionBase<T>, IList<T>
|
|
{
|
|
#region IList<T> Members
|
|
|
|
/// <summary>
|
|
/// IndexOf.
|
|
/// </summary>
|
|
/// <remarks>Consider reimplementing this method for better performance.</remarks>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public virtual int IndexOf(T item)
|
|
{
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
if (object.Equals(this[i], item))
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Insert.
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="item"></param>
|
|
public void Insert(int index, T item)
|
|
{
|
|
MethodModifiesCollection();
|
|
}
|
|
|
|
/// <summary>
|
|
/// RemoveAt.
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
public void RemoveAt(int index)
|
|
{
|
|
MethodModifiesCollection();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indexer.
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public T this[int index]
|
|
{
|
|
get { return GetItem(index); }
|
|
set { MethodModifiesCollection(); }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the item at the given <paramref name="index"/>.
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
protected abstract T GetItem(int index);
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override IEnumerator<T> GetEnumerator()
|
|
{
|
|
return GetEnumerable().GetEnumerator();
|
|
}
|
|
|
|
private IEnumerable<T> GetEnumerable()
|
|
{
|
|
for (int i = 0; i < Count; i++)
|
|
{
|
|
yield return GetItem(i);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|