Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use reflection to determine the nested type (element type) of an array?

I have an instance of System.Type, for which "IsArray" returns true.

How can I determine the "nested type" of the array type?

i.e.

Type GetArrayType(Type t) {     if(t.IsArray)     {         //  What to put here?     }     throw new Exception("Type is not an array"); } Assert.That(GetArrayType(typeof(string[])), Iz.EqualTo(typeof(string)); Assert.That(GetArrayType(typeof(Foo[])), Iz.EqualTo(typeof(Foo)); 
like image 758
Paul Hollingsworth Avatar asked May 08 '09 17:05

Paul Hollingsworth


People also ask

How do you find the type of an element in an array?

Well, you can get the element type of the array: Type type = array. GetType(). GetElementType();

How do I get TypeInfo?

To get a TypeInfo object from a Type object, use the IntrospectionExtensions. GetTypeInfo(Type) extension method. A TypeInfo object represents the type definition itself, whereas a Type object represents a reference to the type definition. Getting a TypeInfo object forces the assembly that contains that type to load.


1 Answers

t.GetElementType()  

Reference.

like image 98
swilliams Avatar answered Sep 22 '22 08:09

swilliams