Home All Groups Group Topic Archive Search About

How do I get a Type instance that represents an array of a given Type instance?

Author
13 Oct 2005 2:01 PM
Jeff Brown
Hi,

I have the following scenario. (The example code is C#.)

    Type elementType = (given);
    Type arrayType = (an array of whatever type "elementType" represents);

The question is, how do I get that value that's assigned to arrayType?

The following code works, but it seems kludgy, since it involves a string
parsing and lookup at runtime.

    Type arrayType = Type.GetType(elementType.FullName + "[]");

Does anyone know of a better way?

Thanks,
Jeff Brown

Author
13 Oct 2005 5:39 PM
Mattias Sjögren
Jeff,

>    Type arrayType = Type.GetType(elementType.FullName + "[]");
>
>Does anyone know of a better way?

That's the way to do it in v1.x of the framework. Note that you may
have to append the assembly name as well.

In v2.0 there's a new Type.MakeArrayType() method for this.


Mattias

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

AddThis Social Bookmark Button