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!