📄 algorithms.cs
字号:
/// </summary>
/// <param name="wrappedEnumerator">IEnumerator to wrap.</param>
public TypedEnumerator(IEnumerator wrappedEnumerator)
{
this.wrappedEnumerator = wrappedEnumerator;
}
T IEnumerator<T>.Current
{
get { return (T) wrappedEnumerator.Current; }
}
void IDisposable.Dispose()
{
if (wrappedEnumerator is IDisposable)
((IDisposable)wrappedEnumerator).Dispose();
}
object IEnumerator.Current
{
get { return wrappedEnumerator.Current; }
}
bool IEnumerator.MoveNext()
{
return wrappedEnumerator.MoveNext();
}
void IEnumerator.Reset()
{
wrappedEnumerator.Reset();
}
}
/// <summary>
/// The class that provides a typed IEnumerable<T> view
/// onto an untyped IEnumerable interface.
/// </summary>
[Serializable]
private class TypedEnumerable<T> : IEnumerable<T>
{
private IEnumerable wrappedEnumerable;
/// <summary>
/// Create a typed IEnumerable<T> view
/// onto an untyped IEnumerable interface.
/// </summary>
/// <param name="wrappedEnumerable">IEnumerable interface to wrap.</param>
public TypedEnumerable(IEnumerable wrappedEnumerable)
{
this.wrappedEnumerable = wrappedEnumerable;
}
public IEnumerator<T> GetEnumerator()
{
return new TypedEnumerator<T>(wrappedEnumerable.GetEnumerator());
}
IEnumerator IEnumerable.GetEnumerator()
{
return wrappedEnumerable.GetEnumerator();
}
}
/// <summary>
/// Given a non-generic IEnumerable interface, wrap a generic IEnumerable<T>
/// interface around it. The generic interface will enumerate the same objects as the
/// underlying non-generic collection, but can be used in places that require a generic interface.
/// The underlying non-generic collection must contain only items that
/// are of type <paramref name="T"/> or a type derived from it. This method is useful
/// when interfacing older, non-generic collections to newer code that uses generic interfaces.
/// </summary>
/// <remarks>Some collections implement both generic and non-generic interfaces. For efficiency,
/// this method will first attempt to cast <paramref name="untypedCollection"/> to IEnumerable<T>.
/// If that succeeds, it is returned; otherwise, a wrapper object is created.</remarks>
/// <typeparam name="T">The item type of the wrapper collection.</typeparam>
/// <param name="untypedCollection">An untyped collection. This collection should only contain
/// items of type <paramref name="T"/> or a type derived from it. </param>
/// <returns>A generic IEnumerable<T> wrapper around <paramref name="untypedCollection"/>.
/// If <paramref name="untypedCollection"/> is null, then null is returned.</returns>
public static IEnumerable<T> TypedAs<T>(IEnumerable untypedCollection)
{
if (untypedCollection == null)
return null;
else if (untypedCollection is IEnumerable<T>)
return (IEnumerable<T>)untypedCollection;
else
return new TypedEnumerable<T>(untypedCollection);
}
/// <summary>
/// The class that provides a typed ICollection<T> view
/// onto an untyped ICollection interface. The ICollection<T>
/// is read-only.
/// </summary>
[Serializable]
private class TypedCollection<T> : ICollection<T>
{
private ICollection wrappedCollection;
/// <summary>
/// Create a typed ICollection<T> view
/// onto an untyped ICollection interface.
/// </summary>
/// <param name="wrappedCollection">ICollection interface to wrap.</param>
public TypedCollection(ICollection wrappedCollection)
{
this.wrappedCollection = wrappedCollection;
}
/// <summary>
/// Throws an NotSupportedException stating that this collection cannot be modified.
/// </summary>
private void MethodModifiesCollection()
{
throw new NotSupportedException(string.Format(Strings.CannotModifyCollection, "strongly-typed Collection"));
}
public void Add(T item)
{ MethodModifiesCollection(); }
public void Clear()
{ MethodModifiesCollection(); }
public bool Remove(T item)
{ MethodModifiesCollection(); return false; }
public bool Contains(T item)
{
IEqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
foreach (object obj in wrappedCollection) {
if (obj is T && equalityComparer.Equals(item, (T)obj))
return true;
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{ wrappedCollection.CopyTo(array, arrayIndex); }
public int Count
{
get { return wrappedCollection.Count; }
}
public bool IsReadOnly
{
get { return true; }
}
public IEnumerator<T> GetEnumerator()
{ return new TypedEnumerator<T>(wrappedCollection.GetEnumerator()); }
IEnumerator IEnumerable.GetEnumerator()
{ return wrappedCollection.GetEnumerator(); }
}
/// <summary>
/// Given a non-generic ICollection interface, wrap a generic ICollection<T>
/// interface around it. The generic interface will enumerate the same objects as the
/// underlying non-generic collection, but can be used in places that require a generic interface.
/// The underlying non-generic collection must contain only items that
/// are of type <paramref name="T"/> or a type derived from it. This method is useful
/// when interfacing older, non-generic collections to newer code that uses generic interfaces.
/// </summary>
/// <remarks><para>Some collections implement both generic and non-generic interfaces. For efficiency,
/// this method will first attempt to cast <paramref name="untypedCollection"/> to ICollection<T>.
/// If that succeeds, it is returned; otherwise, a wrapper object is created.</para>
/// <para>Unlike the generic interface, the non-generic ICollection interfaces does
/// not contain methods for adding or removing items from the collection. For this reason,
/// the returned ICollection<T> will be read-only.</para></remarks>
/// <typeparam name="T">The item type of the wrapper collection.</typeparam>
/// <param name="untypedCollection">An untyped collection. This collection should only contain
/// items of type <paramref name="T"/> or a type derived from it. </param>
/// <returns>A generic ICollection<T> wrapper around <paramref name="untypedCollection"/>.
/// If <paramref name="untypedCollection"/> is null, then null is returned.</returns>
public static ICollection<T> TypedAs<T>(ICollection untypedCollection)
{
if (untypedCollection == null)
return null;
else if (untypedCollection is ICollection<T>)
return (ICollection<T>) untypedCollection;
else
return new TypedCollection<T>(untypedCollection);
}
/// <summary>
/// The class used to create a typed IList<T> view onto
/// an untype IList interface.
/// </summary>
[Serializable]
private class TypedList<T> : IList<T>
{
private IList wrappedList;
/// <summary>
/// Create a typed IList<T> view onto
/// an untype IList interface.
/// </summary>
/// <param name="wrappedList">The IList to wrap.</param>
public TypedList(IList wrappedList)
{
this.wrappedList = wrappedList;
}
public IEnumerator<T> GetEnumerator()
{ return new TypedEnumerator<T>(wrappedList.GetEnumerator()); }
IEnumerator IEnumerable.GetEnumerator()
{ return wrappedList.GetEnumerator(); }
public int IndexOf(T item)
{ return wrappedList.IndexOf(item); }
public void Insert(int index, T item)
{ wrappedList.Insert(index, item); }
public void RemoveAt(int index)
{ wrappedList.RemoveAt(index); }
public void Add(T item)
{ wrappedList.Add(item); }
public void Clear()
{ wrappedList.Clear(); }
public bool Contains(T item)
{ return wrappedList.Contains(item); }
public void CopyTo(T[] array, int arrayIndex)
{ wrappedList.CopyTo(array, arrayIndex); }
public T this[int index]
{
get { return (T)wrappedList[index]; }
set { wrappedList[index] = value; }
}
public int Count
{
get { return wrappedList.Count ; }
}
public bool IsReadOnly
{
get { return wrappedList.IsReadOnly; }
}
public bool Remove(T item)
{
if (wrappedList.Contains(item)) {
wrappedList.Remove(item);
return true;
}
else {
return false;
}
}
}
/// <summary>
/// Given a non-generic IList interface, wrap a generic IList<T>
/// interface around it. The generic interface will enumerate the same objects as the
/// underlying non-generic list, but can be used in places that require a generic interface.
/// The underlying non-generic list must contain only items that
/// are of type <paramref name="T"/> or a type derived from it. This method is useful
/// when interfacing older, non-generic lists to newer code that uses generic interfaces.
/// </summary>
/// <remarks>Some collections implement both generic and non-generic interfaces. For efficiency,
/// this method will first attempt to cast <paramref name="untypedList"/> to IList<T>.
/// If that succeeds, it is returned; otherwise, a wrapper object is created.</remarks>
/// <typeparam name="T">The item type of the wrapper list.</typeparam>
/// <param name="untypedList">An untyped list. This list should only contain
/// items of type <paramref name="T"/> or a type derived from it. </param>
/// <returns>A generic IList<T> wrapper around <paramref name="untypedlist"/>.
/// If <paramref name="untypedlist"/> is null, then null is returned.</returns>
public static IList<T> TypedAs<T>(IList untypedList)
{
if (untypedList == null)
return null;
else if (untypedList is IList<T>)
return (IList<T>)untypedList;
else
return new TypedList<T>(untypedList);
}
/// <summary>
/// The class that is used to provide an untyped ICollection
/// view onto a typed ICollection<T> interface.
/// </summary>
[Serializable]
private class UntypedCollection<T> : ICollection
{
private ICollection<T> wrappedCollection;
/// <summary>
/// Create an untyped ICollection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -