471,339 Members | 1,284 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 software developers and data experts.

IsArray?

Hello again,

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

How can I find out, whether the type variable represents System.Array?

The way type.Name == "System.Array" is not really good, isn't it?

Norbert
Jun 27 '08 #1
8 6251
"Norbert Pürringer" <th*******@graffiti.netwrote:
The way type.Name == "System.Array" is not really good, isn't it?
typeof(Array).IsAssignableFrom(type)

Eq.
Jun 27 '08 #2
On Apr 28, 12:07 pm, Norbert Pürringer <thalio...@graffiti.netwrote:
I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.
What exactly do you mean by that? type.IsArray should be fine.

Could you post a short but complete program demonstrating the problem?

Jon
Jun 27 '08 #3
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote:
What exactly do you mean by that? type.IsArray should be fine.
typeof(anything[]).IsArray is true, but typeof(Array).IsArray is false.

Eq.
Jun 27 '08 #4
On Apr 28, 1:39 pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
What exactly do you mean by that? type.IsArray should be fine.

typeof(anything[]).IsArray is true, but typeof(Array).IsArray is false.
As the docs (for IsArray) state: "The Array class returns false
because it is not an array."

Not exactly clear, but my guess is it's because Array is abstract -
you can't have an instance of just Array, and various array-like
things don't make sense for it (such as element type).

Jon
Jun 27 '08 #5
Norbert Pürringer wrote:
Hello again,

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

How can I find out, whether the type variable represents System.Array?
Could to not see if the type "is IEnumerable"? I could be way off here but I
thought most if not all array types were implicitly IEnumerable.

Chris.
Jun 27 '08 #6
Well, Array is abstract, so you will never actually have an Array
instance - just things that subclass it. But if you really must cater
for both T[] and Array, then perhaps just:

bool isArray = type.IsArray || type == typeof(Array);

Marc
Jun 27 '08 #7
Chris Shepherd wrote:
Norbert Pürringer wrote:
>Hello again,

I've got a type variable, e.g.

Type type;

Now I want to know whether the type represents an Array type
(System.Array).

type.IsArray results false because System.Array is an object, not an
array.

How can I find out, whether the type variable represents
System.Array?

Could to not see if the type "is IEnumerable"? I could be way off
here but I thought most if not all array types were implicitly
IEnumerable.
Sure... but so are many things that aren't arrays.
>
Chris.

Jun 27 '08 #8
Ben Voigt [C++ MVP] wrote:
>>How can I find out, whether the type variable represents
System.Array?
Could to not see if the type "is IEnumerable"? I could be way off
here but I thought most if not all array types were implicitly
IEnumerable.

Sure... but so are many things that aren't arrays.
I should have been clearer -- I was mentioning arrays in the sense of the
logical construct vs the literal class Array.

The OP should probably clarify the exact use scenario he's looking for.

Chris.

Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.