How to get Array element type?

M

Michael Bray

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
 
J

Jani Järvinen [MVP]

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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
M

Michael Bray

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

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!
 
D

Dave Sexton

Hi Michael,

Just use the Type.GetElementType method:

return t.GetElementType();

--
Dave Sexton

Michael Bray said:
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

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!
 
D

Dustin Campbell

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.
 
M

Michael Bray

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
 
D

Dustin Campbell

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top