C#系统。数组如何计数或找到它有多少个维度?
如果我有一个作为System.array的未知数组,我如何才能找到它有多少维?我在想一些可以被称为Array.GetDimensionCount()的东西 例如,如果我正在System.Array类上构建扩展方法,并且我想验证arrayDimensionIndex是否大于或等于数组的维数,我想抛出ArgumentOutOfRangeExceptionC#系统。数组如何计数或找到它有多少个维度?,c#,arrays,multidimensional-array,dimensions,C#,Arrays,Multidimensional Array,Dimensions,如果我有一个作为System.array的未知数组,我如何才能找到它有多少维?我在想一些可以被称为Array.GetDimensionCount()的东西 例如,如果我正在System.Array类上构建扩展方法,并且我想验证arrayDimensionIndex是否大于或等于数组的维数,我想抛出ArgumentOutOfRangeException /// <summary> /// Determines whether the value of the speci
/// <summary>
/// Determines whether the value of the specified arrayIndex is within
/// the bounds of this instance for the specified dimension.
/// </summary>
/// <param name="instance">The instance of the array to check.</param>
/// <param name="arrayDimensionIndex">Index of the array dimension.</param>
/// <param name="arrayIndex">The arrayIndex.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// If arrayDimensionIndex exceeds the number of dimesnions in the array.
/// </exception>
public static bool IsInBounds(this Array instance, int arrayDimensionIndex, int arrayIndex)
{
// Determine if this instance has more dimension than the specified array dimension to check.
if(instance.Get?????() <= arrayDimensionIndex)
{
throw new ArgumentOutOfRangeException();
}
// Get the length of the dimension specifed
int arraDimensionLength = instance.GetLength(arrayDimensionIndex);
// Check if the value is withing the array bounds
bool isInBounds = ((uint)arrayIndex < arraDimensionLength);
// Return the result
return isInBounds;
}
//
///确定指定arrayIndex的值是否在
///指定维度的此实例的边界。
///
///要检查的数组的实例。
///数组维度的索引。
///arrayIndex。
///
///
///如果arrayDimensionIndex超过数组中的维度数。
///
公共静态bool IsInBounds(此数组实例,int-arrayDimensionIndex,int-arrayIndex)
{
//确定此实例的维度是否大于要检查的指定数组维度。
如果(instance.Get?????()您要查找的关键字是。使用Rank
属性找出您的数组有多少维度。您要查找多少维度
属性将返回数组中的维度数。对于单个维度数组,将返回值1,以此类推
然后,您可以调用来确定每个维度的长度。您应该能够专门使用Rank.Array.Rank。以下是它的MSDN信息:
如果您有一个函数可以接受任何维度的数组,并且在强制转换之前需要知道它有多少维度,那么您可以使用:
array.GetType().GetArrayRank()
太棒了,谢谢。一旦网站允许,我会接受这个答案。欢迎来到StackOverflow!请看,共识是“不,他们不应该”。