Getting the type of elements in an array of a general type

I have a generic wrapper class that can sometimes get an array type as its generic. I can determine if the supplied generic type is an array property of IsArray. But is there a way to get the type of array elements in the code? I looked at all the public attributes of the Type object and just did not see it.

Example:

public class wrap<T> { public void doSomething() { if (typeof(T).IsArray) Type arrayElementType = typeof(T).??? ; } } // typeof(T) when an array is "int[]" // typeof(T).BaseType is "System.Array" // how to get "int" out of this? 
+4
source share
1 answer

You are looking for the Type.GetElementType method.

When overridden in a derived class, returns the type of the object covered or called the current array, pointer, or reference type.

 Type arrayElementType = typeof(T).GetElementType(); 
+4
source

All Articles