问题
Possible Duplicate:
How do I use reflection to determine the nested type of an array?
I have a class A and trying to get the underlying type of an array in it.
Class A
{
A1[] obja1;
A2[] obja2;
string x;
int i;
}
How do I get the underlying object type of obja1 as A1 and obja2 as A2?Here is the part of the code I have:
object AClass = myAssembly.CreateInstance("A");
PropertyInfo[] pinfos = AClass.GetType().GetProperties();
foreach(PropertyInfo pinfo in pinfos)
{
if(pinfo.PropertyType.IsArray)
{
//here get the the underlying property type so that I can do something as follows
var arr = myAssembly.CreateInstance(typeof(A1), 100);
//need to get if the array is array of A1 or A2 but do not want to hardcode
}
}
Thanks for the help..
回答1:
If I got question correctly. You may use Get Element Type to get element type and compare it with required.
Or
Just use typeof(A1[])
and typeof(A2[])
来源:https://stackoverflow.com/questions/12267676/get-the-type-of-an-array-object