Get the type of an array object [duplicate]

拈花ヽ惹草 提交于 2019-12-08 06:46:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!