,을 위해 유사한 래퍼를 생성하는 것은 매우 쉬워야한다 단순히 목록을 수정 허용하지 IList<T>
에 대한 래퍼입니다 (그래서는 읽기 전용 랩퍼를 작성하는은 IList 필요 없어요) ICollection<T>
:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
class MyReadOnlyCollection<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
{
private ICollection<T> _collection;
private object _syncRoot;
public MyReadOnlyCollection(ICollection<T> collection)
{
_collection = collection;
}
public void Add(T item)
{
throw new NotSupportedException("Trying to modify a read-only collection.");
}
public void Clear()
{
throw new NotSupportedException("Trying to modify a read-only collection.");
}
public bool Contains(T item)
{
return _collection.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_collection.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _collection.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public bool Remove(T item)
{
throw new NotSupportedException("Trying to modify a read-only collection.");
}
public IEnumerator<T> GetEnumerator()
{
return _collection.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((ICollection)_collection).GetEnumerator();
}
public void CopyTo(Array array, int index)
{
((ICollection)_collection).CopyTo(array, index);
}
public bool IsSynchronized
{
get { return false; }
}
public object SyncRoot
{
get
{
if (_syncRoot == null)
{
ICollection list = _collection as ICollection;
if (list != null)
{
_syncRoot = list.SyncRoot;
}
else
{
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
}
}
return _syncRoot;
}
}
}
명명 물건 "은 IList의"를 다시 어렵다 ... 그리고 - 그냥 당신이 원하는 항목의 배열을 제공합니다 ... 당신이 필요 * 어떻게 든 *에 객체 무엇을 이야기합니다 –
을 포함하십시오. nt IEnumerable + IList/ICollection에서 계산하지만 IList의 항목이 아닙니다. – Rup
@Rup : 예, 읽을 수있는 ICollection을 노출해야합니다. – ordag