1

可能重复:
如何使用反射来确定数组的嵌套类型?

我有一个 A 类并试图在其中获取数组的基础类型。

Class A
{
   A1[] obja1;
   A2[] obja2;
   string x;
   int i;

}

如何将 obj1 的底层对象类型作为 A1,将 obj2 的底层对象类型作为 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..  
4

1 回答 1

2

如果我的问题正确。您可以使用Get Element Type来获取元素类型并将其与 required 进行比较。

或者

只需使用typeof(A1[])typeof(A2[])

于 2012-09-04T16:31:29.017 回答