本文整理匯總了C#中System.Array.GetRelative方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.GetRelative方法的具體用法?C# Array.GetRelative怎麽用?C# Array.GetRelative使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.GetRelative方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Copy
// Copy the contents of one array into another (general-purpose version).
public static void Copy(Array sourceArray, int sourceIndex,
Array destinationArray,
int destinationIndex, int length)
{
// Validate the parameters.
if(sourceArray == null)
{
throw new ArgumentNullException("sourceArray");
}
if(destinationArray == null)
{
throw new ArgumentNullException("destinationArray");
}
if(sourceArray.GetRank() != destinationArray.GetRank())
{
throw new RankException(_("Arg_MustBeSameRank"));
}
int srcLower = sourceArray.GetLowerBound(0);
int srcLength = sourceArray.GetLength();
int dstLower = destinationArray.GetLowerBound(0);
int dstLength = destinationArray.GetLength();
if(sourceIndex < srcLower)
{
throw new ArgumentOutOfRangeException
("sourceIndex", _("ArgRange_Array"));
}
if(destinationIndex < dstLower)
{
throw new ArgumentOutOfRangeException
("destinationIndex", _("ArgRange_Array"));
}
if(length < 0)
{
throw new ArgumentOutOfRangeException
("length", _("ArgRange_NonNegative"));
}
int srcRelative = sourceIndex - srcLower;
int dstRelative = destinationIndex - dstLower;
if((srcLength - (srcRelative)) < length ||
(dstLength - (dstRelative)) < length)
{
throw new ArgumentException(_("Arg_InvalidArrayRange"));
}
// Get the array element types.
Type arrayType1 = sourceArray.GetType().GetElementType();
Type arrayType2 = destinationArray.GetType().GetElementType();
// Is this a simple array copy of the same element type?
if(arrayType1 == arrayType2)
{
InternalCopy
(sourceArray, srcRelative,
destinationArray, dstRelative,
length);
return;
}
// Check that casting between the types is possible,
// without using a narrowing conversion.
if(!ArrayTypeCompatible(arrayType1, arrayType2))
{
throw new ArrayTypeMismatchException
(_("Exception_ArrayTypeMismatch"));
}
// Copy the array contents the hard way. We don't have to
// worry about overlapping ranges because there is no way
// to get here if the source and destination are the same.
int index;
for(index = 0; index < length; ++index)
{
try
{
destinationArray.SetRelative(
Convert.ConvertObject(
sourceArray.GetRelative(srcRelative + index),
arrayType2), dstRelative + index);
}
catch(FormatException e)
{
throw new InvalidCastException(String.Format(_("InvalidCast_FromTo"),
arrayType1, arrayType2), e);
}
}
}