473,388 Members | 1,492 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,388 software developers and data experts.

How to get Array element type?

Let's say I am using reflection to analyze my classes, and I have an array
of another class. How do I get the base class that forms the array from a
type?

public class A
{
}
A[] MyArray = new A[];
Type t = MyArray.GetType();

/// FROM THIS POINT, I want to get back to the 'A' type using ONLY my t
/// variable - how?

Type t2 = ??? (I want this to end up being typeof(A))

Thanks!

-mdb

Dec 23 '06 #1
7 5102
Michael,
How do I get the base class that forms the array from a type?
To do this, use the static GetTypeArray method of the System.Type class, and
pass in your array object. This method returns an array of Type objects.
This is because each array element can be potentially be of a different type
(think object[]), but in your case it is probably enough to simply use the
first Type element returned.

Here's an example:

----------------------------
public class A
{
}
....
A[] myArray = new A[2];
myArray[0] = new A();
myArray[1] = new A();
Type[] types = Type.GetTypeArray(myArray);
MessageBox.Show("There are " + types.Length + " elements in the array.");
foreach (Type type in types)
{
MessageBox.Show(type.Name);
// do something with 'type'
}
----------------------------

Note that each element in your array must be non-null when calling
GetTypeArray; otherwise an exception with the message "Value cannot be null"
is raised.

Merry Christmas!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Dec 23 '06 #2
I don't have any objects - I'm analyzing classes in the absense of any
data whatsoever.

In other words, I'm trying to fill in this function:

public Type GetArrayElementType(Type t)
{
// NOTE: t.IsArray == true
// I can't use t.BaseType - that is System.Array
// I can't use t.UnderlyingSystemType - it is the same

return ?????;
}

actually I have a solution now, but I was hoping for something a little
bit cleaner (more direct)... Here's what I did:

public Type GetArrayElementType(Type t)
{
string tName = t.FullName.Replace("[]", string.Empty);
Type elType = t.Assembly.GetType(typeName);

return elType;
}

Any suggestions on a more direct way to get there (without fiddling with
the type name itself)?

-mdb

"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fiwrote in
news:#l**************@TK2MSFTNGP04.phx.gbl:
Michael,
>How do I get the base class that forms the array from a type?

To do this, use the static GetTypeArray method of the System.Type
class, and pass in your array object. This method returns an array of
Type objects. This is because each array element can be potentially be
of a different type (think object[]), but in your case it is probably
enough to simply use the first Type element returned.

Here's an example:

----------------------------
public class A
{
}
...
A[] myArray = new A[2];
myArray[0] = new A();
myArray[1] = new A();
Type[] types = Type.GetTypeArray(myArray);
MessageBox.Show("There are " + types.Length + " elements in the
array."); foreach (Type type in types)
{
MessageBox.Show(type.Name);
// do something with 'type'
}
----------------------------

Note that each element in your array must be non-null when calling
GetTypeArray; otherwise an exception with the message "Value cannot be
null" is raised.

Merry Christmas!
Dec 23 '06 #3
Hi Michael,

Just use the Type.GetElementType method:

return t.GetElementType();

--
Dave Sexton

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcomwrote in message
news:Xn****************************@207.46.248.16. ..
>I don't have any objects - I'm analyzing classes in the absense of any
data whatsoever.

In other words, I'm trying to fill in this function:

public Type GetArrayElementType(Type t)
{
// NOTE: t.IsArray == true
// I can't use t.BaseType - that is System.Array
// I can't use t.UnderlyingSystemType - it is the same

return ?????;
}

actually I have a solution now, but I was hoping for something a little
bit cleaner (more direct)... Here's what I did:

public Type GetArrayElementType(Type t)
{
string tName = t.FullName.Replace("[]", string.Empty);
Type elType = t.Assembly.GetType(typeName);

return elType;
}

Any suggestions on a more direct way to get there (without fiddling with
the type name itself)?

-mdb

"Jani Järvinen [MVP]" <ja***@removethis.dystopia.fiwrote in
news:#l**************@TK2MSFTNGP04.phx.gbl:
>Michael,
>>How do I get the base class that forms the array from a type?

To do this, use the static GetTypeArray method of the System.Type
class, and pass in your array object. This method returns an array of
Type objects. This is because each array element can be potentially be
of a different type (think object[]), but in your case it is probably
enough to simply use the first Type element returned.

Here's an example:

----------------------------
public class A
{
}
...
A[] myArray = new A[2];
myArray[0] = new A();
myArray[1] = new A();
Type[] types = Type.GetTypeArray(myArray);
MessageBox.Show("There are " + types.Length + " elements in the
array."); foreach (Type type in types)
{
MessageBox.Show(type.Name);
// do something with 'type'
}
----------------------------

Note that each element in your array must be non-null when calling
GetTypeArray; otherwise an exception with the message "Value cannot be
null" is raised.

Merry Christmas!

Dec 23 '06 #4
Let's say I am using reflection to analyze my classes, and I have an
array of another class. How do I get the base class that forms the
array from a type?

public class A
{
}
A[] MyArray = new A[];
Type t = MyArray.GetType();
/// FROM THIS POINT, I want to get back to the 'A' type using ONLY my
t /// variable - how?

Type t2 = ??? (I want this to end up being typeof(A))
Type t2 = MyArray.GetType().GetElementType();

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 23 '06 #5
Dustin Campbell <du*****@no-spam-pleasedevexpress.comwrote in
news:c1**************************@news.microsoft.c om:
>Let's say I am using reflection to analyze my classes, and I have an
array of another class. How do I get the base class that forms the
array from a type?

public class A
{
}
A[] MyArray = new A[];
Type t = MyArray.GetType();
/// FROM THIS POINT, I want to get back to the 'A' type using ONLY my
t /// variable - how?

Type t2 = ??? (I want this to end up being typeof(A))

Type t2 = MyArray.GetType().GetElementType();

But I want to do it ONLY having the 't' variable... in other words, I
need to do it dynamically, NOT in static code.

-mdb
Dec 23 '06 #6
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in
news:uq**************@TK2MSFTNGP06.phx.gbl:
Hi Michael,

Just use the Type.GetElementType method:

return t.GetElementType();
Aha!!! The Type itself has a GetElementType... that is perfect!

-mdb

Dec 23 '06 #7
>>Let's say I am using reflection to analyze my classes, and I have an
>>array of another class. How do I get the base class that forms the
array from a type?

public class A
{
}
A[] MyArray = new A[];
Type t = MyArray.GetType();
/// FROM THIS POINT, I want to get back to the 'A' type using ONLY
my
t /// variable - how?
Type t2 = ??? (I want this to end up being typeof(A))
Type t2 = MyArray.GetType().GetElementType();
But I want to do it ONLY having the 't' variable... in other words,
I need to do it dynamically, NOT in static code.
Well, If you have 't' already, it's just this:

Type t2 = t.GetElementType();

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 23 '06 #8

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

Similar topics

6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
21
by: subramanian100in | last post by:
Suppose we have char array; C allows taking the address of array (ie &array is valid) though the element array cannot be accessed. Is this allowed for use in binary search method (as given...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.