Type.GetArrayRank()方法用于获取数组中的维数。
Syntax: public virtual int GetArrayRank ();
Return Value: This method returns an integer which contains the number of dimensions in the current type.
Exception: This method throws ArgumentException if the current type is not an array.
下面的程序说明了Type.GetArrayRank()方法的用法:
范例1:
// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
try
{
// Declaring and intializing Type object
Type type = typeof(int[,,,,, ]);
// Getting the dimensions
// using GetArrayRank()
int rank = type.GetArrayRank();
// Display the rank
Console.WriteLine("ArrayRank is: {0}", rank);
}
catch (ArgumentException e)
{
Console.WriteLine("The current type is not an Array");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
ArrayRank is: 6
范例2:
// C# program to demonstrate the
// Type.GetArrayRank() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
try {
// Declaring and intializing Type object
Type type = typeof(int);
// Getting ArrayRank by
// using GetArrayRank()
int rank = type.GetArrayRank();
// Display the rank
Console.WriteLine("ArrayRank is : {0}", rank);
}
catch (ArgumentException e)
{
Console.WriteLine("The current type is not an Array");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
The current type is not an Array
Exception Thrown: System.ArgumentException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.type.getarrayrank?view=netcore-3.0