📄 collections.cs
字号:
{
if (value < m_count)
value = m_count;
if (value != m_array.Length)
{
if (value > 0)
{
Import[] temp = new Import[value];
Array.Copy(m_array, temp, m_count);
m_array = temp;
}
else
{
m_array = new Import[DEFAULT_CAPACITY];
}
}
}
}
/// <summary>
/// Adds the elements of another <c>ImportCollection</c> to the current <c>ImportCollection</c>.
/// </summary>
/// <param name="x">The <c>ImportCollection</c> whose elements should be added to the end of the current <c>ImportCollection</c>.</param>
/// <returns>The new <see cref="ImportCollection.Count"/> of the <c>ImportCollection</c>.</returns>
public virtual int AddRange(ImportCollection x)
{
if (m_count + x.Count >= m_array.Length)
EnsureCapacity(m_count + x.Count);
Array.Copy(x.m_array, 0, m_array, m_count, x.Count);
m_count += x.Count;
m_version++;
return m_count;
}
/// <summary>
/// Adds the elements of a <see cref="Import"/> array to the current <c>ImportCollection</c>.
/// </summary>
/// <param name="x">The <see cref="Import"/> array whose elements should be added to the end of the <c>ImportCollection</c>.</param>
/// <returns>The new <see cref="ImportCollection.Count"/> of the <c>ImportCollection</c>.</returns>
public virtual int AddRange(Import[] x)
{
if (m_count + x.Length >= m_array.Length)
EnsureCapacity(m_count + x.Length);
Array.Copy(x, 0, m_array, m_count, x.Length);
m_count += x.Length;
m_version++;
return m_count;
}
/// <summary>
/// Sets the capacity to the actual number of elements.
/// </summary>
public virtual void TrimToSize()
{
this.Capacity = m_count;
}
#endregion
#region Implementation (helpers)
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="index"/> is less than zero</para>
/// <para>-or-</para>
/// <para><paramref name="index"/> is equal to or greater than <see cref="ImportCollection.Count"/>.</para>
/// </exception>
private void ValidateIndex(int i)
{
ValidateIndex(i, false);
}
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="index"/> is less than zero</para>
/// <para>-or-</para>
/// <para><paramref name="index"/> is equal to or greater than <see cref="ImportCollection.Count"/>.</para>
/// </exception>
private void ValidateIndex(int i, bool allowEqualEnd)
{
int max = (allowEqualEnd)?(m_count):(m_count-1);
if (i < 0 || i > max)
throw new System.ArgumentOutOfRangeException("Index was out of range. Must be non-negative and less than the size of the collection.", (object)i, "Specified argument was out of the range of valid values.");
}
private void EnsureCapacity(int min)
{
int newCapacity = ((m_array.Length == 0) ? DEFAULT_CAPACITY : m_array.Length * 2);
if (newCapacity < min)
newCapacity = min;
this.Capacity = newCapacity;
}
#endregion
#region Implementation (ICollection)
void ICollection.CopyTo(Array array, int start)
{
this.CopyTo((Import[])array, start);
}
#endregion
#region Implementation (IList)
object IList.this[int i]
{
get { return (object)this[i]; }
set { this[i] = (Import)value; }
}
int IList.Add(object x)
{
return this.Add((Import)x);
}
bool IList.Contains(object x)
{
return this.Contains((Import)x);
}
int IList.IndexOf(object x)
{
return this.IndexOf((Import)x);
}
void IList.Insert(int pos, object x)
{
this.Insert(pos, (Import)x);
}
void IList.Remove(object x)
{
this.Remove((Import)x);
}
void IList.RemoveAt(int pos)
{
this.RemoveAt(pos);
}
#endregion
#region Implementation (IEnumerable)
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)(this.GetEnumerator());
}
#endregion
#region Nested enumerator class
/// <summary>
/// Supports simple iteration over a <see cref="ImportCollection"/>.
/// </summary>
private class Enumerator : IEnumerator, IImportCollectionEnumerator
{
#region Implementation (data)
private ImportCollection m_collection;
private int m_index;
private int m_version;
#endregion
#region Construction
/// <summary>
/// Initializes a new instance of the <c>Enumerator</c> class.
/// </summary>
/// <param name="tc"></param>
internal Enumerator(ImportCollection tc)
{
m_collection = tc;
m_index = -1;
m_version = tc.m_version;
}
#endregion
#region Operations (type-safe IEnumerator)
/// <summary>
/// Gets the current element in the collection.
/// </summary>
public Import Current
{
get { return m_collection[m_index]; }
}
/// <summary>
/// Advances the enumerator to the next element in the collection.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The collection was modified after the enumerator was created.
/// </exception>
/// <returns>
/// <c>true</c> if the enumerator was successfully advanced to the next element;
/// <c>false</c> if the enumerator has passed the end of the collection.
/// </returns>
public bool MoveNext()
{
if (m_version != m_collection.m_version)
throw new System.InvalidOperationException("Collection was modified; enumeration operation may not execute.");
++m_index;
return (m_index < m_collection.Count) ? true : false;
}
/// <summary>
/// Sets the enumerator to its initial position, before the first element in the collection.
/// </summary>
public void Reset()
{
m_index = -1;
}
#endregion
#region Implementation (IEnumerator)
object IEnumerator.Current
{
get { return (object)(this.Current); }
}
#endregion
}
#endregion
#region Nested Syncronized Wrapper class
[Serializable]
private class SyncImportCollection : ImportCollection, System.Runtime.Serialization.IDeserializationCallback
{
#region Implementation (data)
private const int timeout = 0; // infinite
private ImportCollection collection;
[NonSerialized]
private System.Threading.ReaderWriterLock rwLock;
#endregion
#region Construction
internal SyncImportCollection(ImportCollection list) : base(Tag.Default)
{
rwLock = new System.Threading.ReaderWriterLock();
collection = list;
}
#endregion
#region IDeserializationCallback Members
void System.Runtime.Serialization.IDeserializationCallback.OnDeserialization(object sender)
{
rwLock = new System.Threading.ReaderWriterLock();
}
#endregion
#region Type-safe ICollection
public override void CopyTo(Import[] array)
{
rwLock.AcquireReaderLock(timeout);
try
{
collection.CopyTo(array);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
public override void CopyTo(Import[] array, int start)
{
rwLock.AcquireReaderLock(timeout);
try
{
collection.CopyTo(array, start);
}
finally
{
rwLock.ReleaseReaderLock();
}
}
public override int Count
{
get
{
int count = 0;
rwLock.AcquireReaderLock(timeout);
try
{
count = collection.Count;
}
finally
{
rwLock.ReleaseReaderLock();
}
return count;
}
}
public override bool IsSynchronized
{
get { return true; }
}
public override object SyncRoot
{
get { return collection.SyncRoot; }
}
#endregion
#region Type-safe IList
public override Import this[int i]
{
get
{
Import thisItem;
rwLock.AcquireReaderLock(timeout);
try
{
thisItem = collection[i];
}
finally
{
rwLock.ReleaseReaderLock();
}
return thisItem;
}
set
{
rwLock.AcquireWriterLock(timeout);
try
{
collection[i] = value;
}
finally
{
rwLock.ReleaseWriterLock();
}
}
}
public override int Add(Import x)
{
int result = 0;
rwLock.AcquireWriterLock(timeout);
try
{
result = collection.Add(x);
}
finally
{
rwLock.ReleaseWriterLock();
}
return result;
}
public override void Clear()
{
rwLock.AcquireWriterLock(timeout);
try
{
collection.Clear();
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public override bool Contains(Import x)
{
bool result = false;
rwLock.AcquireReaderLock(timeout);
try
{
result = collection.Contains(x);
}
finally
{
rwLock.ReleaseReaderLock();
}
return result;
}
public override int IndexOf(Import x)
{
int result = 0;
rwLock.AcquireReaderLock(timeout);
try
{
result = collection.IndexOf(x);
}
finally
{
rwLock.ReleaseReaderLock();
}
return result;
}
public override void Insert(int pos, Import x)
{
rwLock.AcquireWriterLock(timeout);
try
{
collection.Insert(pos,x);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public override void Remove(Import x)
{
rwLock.AcquireWriterLock(timeout);
try
{
collection.Remove(x);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public override void RemoveAt(int pos)
{
rwLock.AcquireWriterLock(timeout);
try
{
collection.RemoveAt(pos);
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public override bool IsFixedSize
{
get { return collection.IsFixedSize; }
}
public override bool IsReadOnly
{
get { return collection.IsReadOnly; }
}
#endregion
#region Type-safe IEnumerable
public override IImportCollectionEnumerator GetEnumerator()
{
IImportCollectionEnumerator enumerator = null;
rwLock.AcquireReaderLock(timeout);
try
{
enumerator = collection.GetEnumerator();
}
finally
{
rwLock.ReleaseReaderLock();
}
return enumerator;
}
#endregion
#region Public Helpers
// (just to mimic some nice features of ArrayList)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -