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

J

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
 
M

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top