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

16,059
t.GetElementType() 

Reference.

Share:
16,059
Paul Hollingsworth
Author by

Paul Hollingsworth

I'm a software developer working in financial services in London. Previously I did a lot of C++ - but lately have been doing a lot of .NET and functional programming. Projects: http://patient0.github.com/FirstClassLisp/

Updated on June 25, 2022

Comments

  • Paul Hollingsworth
    Paul Hollingsworth almost 2 years

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