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

  • Thread starter Thread starter Jeff Brown
  • Start date Start date
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
 
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
 
Back
Top