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

C#.NetReflection

C# Problem Overview


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));

C# Solutions


Solution 1 - C#

t.GetElementType() 

Reference.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul HollingsworthView Question on Stackoverflow
Solution 1 - C#swilliamsView Answer on Stackoverflow