获取数组对象的类型 [英] Get the type of an array object

查看:22
本文介绍了获取数组对象的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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;

}

如何将obja1的底层对象类型设为A1,将obja2设为A2?以下是我拥有的部分代码:

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

推荐答案

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

If I got question correctly. You may use Get Element Type to get element type and compare it with required.

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

这篇关于获取数组对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆