BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Reflecting Array Information

The fourteenth part of the Reflection tutorial looks at the reflection of array types. This includes obtaining the underlying type for an array, the number of dimensions and the types of the individual elements in an array of objects.

Arrays

When you are using reflection to obtain the details of a type, you will sometimes retrieve details of a member that includes an array type. You can further examine these using members of the System.Type class that are specific to arrays. These methods allow you to retrieve the underlying type declared for any array items, or to find the number of dimensions in the array. You can also use reflection against an object array instance to find the types of each individual element of that array. All of these functions can be used against single-dimension arrays, multidimensional arrays and arrays containing other arrays, otherwise known as jagged arrays.

To reflect over an array type you use the same processes as for reflecting over any other type. For example, the code below obtains a Type instance for a two-dimensional string array using the typeof keyword. It then outputs the type name and the value of the IsArray flag, which indicates that the originating type was, indeed, an array.

Type type = typeof(string[,]);
Console.WriteLine(type.Name);     // String[,]
Console.WriteLine(type.IsArray);  // True

Obtaining the Number of Dimensions in an Array

A simple reflection task is to find out how many dimensions there are in an array type. You can do this using the GetArrayRank method of the Type instance. No parameters are used with this method, which returns the number of dimensions as an integer.

The code below uses GetArrayRank against a Type object for a two-dimensional array.

Type type = typeof(string[,]);
Console.WriteLine(type.GetArrayRank());  // 2

Jagged arrays can be used to represent multidimensional data structures but are not necessarily multidimensional arrays. The code below demonstrates this. Here we have a type that can hold a jagged array. In reality, this is just an array of arrays so the GetArrayRank method returns the number of dimensions in the outer array, which is one.

Type type = typeof(string[][]);
Console.WriteLine(type.GetArrayRank());  // 1

Obtaining the Element Type for an Array

If you need to know the declared type of the elements that an array holds, you can use the GetElementType method. This returns a Type instance, as shown below:

Type type = typeof(string[,]);
Console.WriteLine(type.GetElementType());  // System.String

As jagged arrays are simply arrays containing arrays, using the method returns the inner array type, as shown below.

Type type = typeof(string[][]);
Console.WriteLine(type.GetElementType());  // System.String[]

If you need to find the underlying type of the innermost array you can use a while loop that retrieves the type using GetElementType repeatedly until the found type's IsArray flag is false.

Type type = typeof(int[][][][]);
while (type.IsArray)
{
    type = type.GetElementType();
}
Console.WriteLine(type.Name);  // Int32

Obtaining the Types of the Items in an Array

The last method of the Type class that we will look at in this article is GetTypeArray. It allows you to obtain the types of the elements in an array instance. This is not the declared type of the array but the actual types of the individual items. GetTypeArray is a static method of the Type class. You provide the object array as a single argument.

In the final code sample we create an array containing a string, a character and an integer, and pass this array to GetTypeArray. The method returns a Type array containing the element types. Note that the types appear in the same order as their originating values in the object array.

object[] myObjects = new object[3];
myObjects[0] = "A string";
myObjects[1] = 'X';
myObjects[2] = 123;
Type[] myObjectTypes = Type.GetTypeArray(myObjects);

/* TYPES

String
Char
Int32

*/
5 June 2012