获取数组对象
问题描述:
可能重复的类型:
How do I use reflection to determine the nested type of an array?获取数组对象
我有一个A类和试图获得的基本类型在它的阵列。
Class A
{
A1[] obja1;
A2[] obja2;
string x;
int i;
}
我如何为A1 obja1的基本对象类型和obja2为A2这里是我的代码的一部分:
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..
通过我的阅读,这个问题是问如何创建数组类型,而不是元素类型的实例,所以它不一定是链接的副本题。要创建一个数组,使用'if(pinfo.PropertyType.IsArray){var arr = Array.CreateInstance(pinfo.PropertyType.GetElementType,elementCount); ...其中elementCount是一个保存数组所需长度的整型变量。 – phoog
非常感谢你..这是非常愚蠢的我完全忘了GetElementType – user799891