473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Getting the underlying Type of an Array Type

Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
if( !arrayType.IsArray )
throw exception

//get the underlying type and return it
}

In Java you can call Class.getComponentType()? Is there something similiar in C#?

thanks,

~harris
Nov 15 '05 #1
4 3130
Harris <mh********@mail.com> wrote:
Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
if( !arrayType.IsArray )
throw exception

//get the underlying type and return it
}

In Java you can call Class.getComponentType()? Is there something similiar in C#?


Type.GetElementType does what you want, I believe.

--
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
Hi Harris,

Actually, The Type object itself holds the information of the underlying
type that
it represents.
So let's assume that your underlying array type is an array of strings.

String[] myStringArray;

typeof(myStringArray).IsArray --- returns true
typeof(myStringArray) ----- is the Type object that represents the
underlying type
typeof(myStringArray).ToString() --- returns System.String[]

So in your example, arrayType itself holds the information on the underlying
array type.

Regards,
Aravind C
"Harris" <mh********@mail.com> wrote in message
news:7d**************************@posting.google.c om...
Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
if( !arrayType.IsArray )
throw exception

//get the underlying type and return it
}

In Java you can call Class.getComponentType()? Is there something similiar in C#?
thanks,

~harris

Nov 15 '05 #3
Hi Harris,

I just realised that you probably wanted to get the underlying type of the
element in the array after reading Jon's post now.

As Jon, suggested,
arrayType.GetElementType() would return the type of the elements
contained in the array.

Regards,
Aravind C
"Harris" <mh********@mail.com> wrote in message
news:7d**************************@posting.google.c om...
Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
if( !arrayType.IsArray )
throw exception

//get the underlying type and return it
}

In Java you can call Class.getComponentType()? Is there something similiar in C#?
thanks,

~harris

Nov 15 '05 #4
Thanks Jon.... somehow I just couldn't see that last night in the doc! :-)

~harris

Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Harris <mh********@mail.com> wrote:
Does anyone know how to get the underlying type of an Array type?

Here's a bit of code demonstrating what I'm looking for...

public Type getUnderlyingType( Type arrayType )
{
if( !arrayType.IsArray )
throw exception

//get the underlying type and return it
}

In Java you can call Class.getComponentType()? Is there something similiar in C#?


Type.GetElementType does what you want, I believe.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

80
by: Bibby | last post by:
Hi, I'm interested in getting started in the programming world. I've dabbled in C, C++ and VB6. Which would be the best language to focus my attention to regarding the following considerations: ...
21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
26
by: Rajeev Tipnis | last post by:
http://support.microsoft.com/default.aspx?scid=kb;en- us;819450 Questions: 1) Is this patch (fix) applicable to the .NET 1.1 Framework as well? That is, if we have Framework 1.1 (On...
3
by: Rahul Anand | last post by:
As per our requirements we have a web service which internally connects (Simple HTTP Post Request) to a remote server to initiate some work. We are calling the web service method asynchronously...
2
by: Jon Turner | last post by:
How do you get an underlying Error number from an exception. I want to test for error numbers instead of strings ? Many TIA's
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
6
by: sathyashrayan | last post by:
Dear Group, Please look at the following demo link. http://www.itsravi.com/demo/new_pms/admin/addproject.php
7
by: Maciej Oszutowski | last post by:
Hi, I'm going to port my PE manipulation library (written in C) to managed c++ class library. I would like to have opportunity to read content not only from files, but also from memory, for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.