/// <summary>
 /// 从特定的 <see cref="System.Array"/> 索引处开始,
 /// 将 <see cref="LabelCollection"/> 的元素复制到一个 <see cref="System.Array"/> 中。
 /// </summary>
 /// <param name="array">作为从 <see cref="LabelCollection"/>
 /// 复制的元素的目标位置的一维 <see cref="System.Array"/>。
 /// <see cref="System.Array"/> 必须具有从零开始的索引。</param>
 /// <param name="arrayIndex"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="arrayIndex"/> 小于零。</exception>
 /// <exception cref="System.ArgumentException">
 /// <paramref name="array"/> 是多维的。</exception>
 /// <exception cref="System.ArgumentException">源 <see cref="LabelCollection"/>
 /// 中的元素数目大于从 <paramref name="arrayIndex"/> 到目标 <paramref name="array"/>
 /// 末尾之间的可用空间。</exception>
 public void CopyTo(string[] array, int arrayIndex)
 {
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     if (array.Rank != 1)
     {
         throw CommonExceptions.MultidimensionalArrayNotSupported("array");
     }
     if (array.GetLowerBound(0) != 0)
     {
         throw CommonExceptions.ArrayNonZeroLowerBound("array");
     }
     if (arrayIndex < 0)
     {
         throw CommonExceptions.ArgumentOutOfRange("arrayIndex", arrayIndex);
     }
     if (array.Length - arrayIndex < this.Count)
     {
         throw CommonExceptions.ArrayTooSmall("array");
     }
     foreach (LexerContext context in this.contexts)
     {
         array[arrayIndex++] = context.Label;
     }
 }
Beispiel #2
0
 /// <summary>
 /// 从特定的 <see cref="Array"/> 索引处开始,将指定集合
 /// 的元素复制到一个 <see cref="Array"/> 中。
 /// </summary>
 /// <param name="source">要复制元素的集合。</param>
 /// <param name="array">从 <paramref name="source"/> 复制的元素的目标位置的一维
 /// <see cref="Array"/>。<paramref name="array"/> 必须具有从零开始的索引。</param>
 /// <param name="index"><paramref name="array"/> 中从零开始的索引,在此处开始复制。</param>
 /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> 小于 <c>0</c>。</exception>
 /// <exception cref="ArgumentException"><paramref name="array"/> 是多维的。</exception>
 /// <exception cref="ArgumentException"><paramref name="source"/>
 /// 中的元素数目大于从 <paramref name="index"/> 到目标 <paramref name="array"/>
 /// 末尾之间的可用空间。</exception>
 /// <exception cref="ArgumentException"><paramref name="source"/>
 /// 的类型无法自动转换为目标 <paramref name="array"/> 的类型。</exception>
 public static void CopyTo <T>(ICollection <T> source, Array array, int index)
 {
     Contract.Requires(source != null);
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     if (array.Rank != 1)
     {
         throw CommonExceptions.ArrayRankMultiDimNotSupported();
     }
     if (array.GetLowerBound(0) != 0)
     {
         throw CommonExceptions.ArrayNonZeroLowerBound("array");
     }
     if (index < 0)
     {
         throw CommonExceptions.ArgumentNegative("index", index);
     }
     if (array.Length - index < source.Count)
     {
         throw CommonExceptions.ArrayTooSmall("array");
     }
     Contract.EndContractBlock();
     T[] arr = array as T[];
     if (arr != null)
     {
         foreach (T obj in source)
         {
             arr[index++] = obj;
         }
     }
     else
     {
         try
         {
             foreach (T obj in source)
             {
                 array.SetValue(obj, index++);
             }
         }
         catch (InvalidCastException ex)
         {
             throw CommonExceptions.InvalidArrayType(ex);
         }
     }
 }