Skip to content

Instantly share code, notes, and snippets.

@derjabkin
Created July 10, 2013 13:26
Show Gist options
  • Save derjabkin/5966269 to your computer and use it in GitHub Desktop.
Save derjabkin/5966269 to your computer and use it in GitHub Desktop.
IList<T> covariance wrapper
public class ListWrapper<TSource, TDest> : IList<TDest>
where TDest : class
where TSource : TDest
{
private readonly IList<TSource> list;
public ListWrapper(IList<TSource> list)
{
this.list = list;
}
public int IndexOf(TDest item)
{
return list.IndexOf((TSource)item);
}
public void Insert(int index, TDest item)
{
list.Insert(index, (TSource)item);
}
public void RemoveAt(int index)
{
list.RemoveAt(index);
}
public TDest this[int index]
{
get { return list[index]; }
set { list[index] = (TSource)value; }
}
public void Add(TDest item)
{
list.Add((TSource)item);
}
public void Clear()
{
list.Clear();
}
public bool Contains(TDest item)
{
return list.Contains((TSource)item);
}
public void CopyTo(TDest[] array, int arrayIndex)
{
list.Cast<TDest>().ToList().CopyTo(array, arrayIndex);
}
public int Count
{
get { return list.Count; }
}
public bool IsReadOnly
{
get { return list.IsReadOnly; }
}
public bool Remove(TDest item)
{
return list.Remove((TSource)item);
}
public IEnumerator<TDest> GetEnumerator()
{
return list.Cast<TDest>().GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment