By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,212 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,212 IT Pros & Developers. It's quick & easy.

Creating typed arrays using reflection

P: n/a
Hi,

I'd like to create typed arrays during runtime, sort of
like this:

Array vals = Array.CreateInstance(objType, noElements);

The problem is that objType will not be a "base" type
like Double or Int32, but rather an array type, like
Double[] or Int32[]. Is there any way of creating, say,
an array of doubles from the double[] system type?
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ranier Dunno <eb*****@hotmail.com> wrote:
I'd like to create typed arrays during runtime, sort of
like this:

Array vals = Array.CreateInstance(objType, noElements);

The problem is that objType will not be a "base" type
like Double or Int32, but rather an array type, like
Double[] or Int32[]. Is there any way of creating, say,
an array of doubles from the double[] system type?


Yup - you just need to get the element type, which you can do with
Type.GetElementType. In other words:

Array vals = Array.CreateInstance (objType.GetElementType(),
noElements);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
-----Original Message-----
Ranier Dunno <eb*****@hotmail.com> wrote:
I'd like to create typed arrays during runtime, sort of like this:

Array vals = Array.CreateInstance(objType, noElements);

The problem is that objType will not be a "base" type
like Double or Int32, but rather an array type, like
Double[] or Int32[]. Is there any way of creating, say, an array of doubles from the double[] system type?
Yup - you just need to get the element type, which you

can do withType.GetElementType. In other words:

Array vals = Array.CreateInstance (objType.GetElementType (), noElements);


Ah, so there was a method - thanks :-)
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.