当前位置: 首页>>代码示例>>C#>>正文


C# Array.SetRelative方法代码示例

本文整理汇总了C#中System.Array.SetRelative方法的典型用法代码示例。如果您正苦于以下问题:C# Array.SetRelative方法的具体用法?C# Array.SetRelative怎么用?C# Array.SetRelative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Array的用法示例。


在下文中一共展示了Array.SetRelative方法的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);
			}
		}
	}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:87,代码来源:Array.cs


注:本文中的System.Array.SetRelative方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。