我的方法如下:MyOneClass one = new MyOneClass("X"); Type t = one.GetType(); PropertyInfo pif = t.GetProperty("GetAL"); object obj = pif.GetValue(one,null); ArrayList al = (ArrayList)obj;

MessageBox.Show(al[0].ToString()); MessageBox.Show(al[1].ToString());
///类public class MyOneClass
{
public string varName;
private ArrayList al = new ArrayList();
public ArrayList GetAL
{
get
{
return this.al;
}
}
public MyOneClass(string varName)
{
this.al.Add("1");
this.al.Add("2");
this.al.Add(varName);
}
}

解决方案 »

  1.   

    why not??????ArrayList tmp = Class1.array;//array是这个属性
      

  2.   

    接你的代码
    ArrayList al = one.GetAL;
      

  3.   

    类class1,其中有属性dpublic class Class1
    {
    public Class1(){} private string ma = "";
    private string mb = ""; private ArrayList md = new ArrayList();
    private Hashtable me = new Hashtable();

    public string a
    {
    get{return ma;} set{ma = value;}
    } public string b
    {
    get{return mb;} set{mb = value;}
    } public ArrayList d
    {
    get{return md;} set{md = value;}
    } public Hashtable e
    {
    get{return me;} set{me = value;}
    }
    }Class1 cls = new Class1();Type t = a.GetType();
    foreach(PropertyInfo p in t.GetProperties())
    {
             object obj = p.GetValue(a,null)
    }然后取道ArrayList的时候,总是只取了一个Count值,并没有取道ArrayList的对象
      

  4.   

    比如
    class MyClass
    {
     public ArrayList List;
    }
    要得到List属性可以这么写:
    Type t = typeof(MyClass);
    MemberInfo[] members = t.GetMember("List");
    Console.WriteLine(members[0].Name);
    ...但是不知道你的意图究竟如何
      

  5.   

    To:brightheroes(闭关|那一剑的风情) 好像你上面的那种方式不行,不管怎么取,只有[0]有值,而且应该是ArrayList的count值
      

  6.   

    Type t = a.GetType();
    foreach(PropertyInfo p in t.GetProperties())
    {
             object obj = p.GetValue(a,null)
    }////////////////////////////////////////你这么写当然得不到了
    对象都没有参与运算,如何得到实际内容?
    为什么不直接使用cls.xxx来取得呢,而非得使用反射
      

  7.   

    using System;
    using System.Collections;
    using  System.Reflection;public class MyClass
    {
    public static void Main()
    {

    Class1 cls = new Class1();
    cls.d.Add("a");
    cls.d.Add("b"); Type t = cls.GetType();
    foreach(PropertyInfo p in t.GetProperties())
    {
              object obj = p.GetValue(cls,null);
    if(obj is ArrayList)
    {
    Console.WriteLine(p.Name);
    Console.WriteLine(((ArrayList)obj).Count);
    }
    }
    Console.Read();
    }


    }public class Class1
    {
    public Class1(){} private string ma = "";
    private string mb = ""; private ArrayList md = new ArrayList();
    private Hashtable me = new Hashtable();

    public string a
    {
    get{return ma;} set{ma = value;}
    } public string b
    {
    get{return mb;} set{mb = value;}
    } public ArrayList d
    {
    get{return md;} set{md = value;}
    } public Hashtable e
    {
    get{return me;} set{me = value;}
    }
    }
      

  8.   

    如果是需要动态的调用对象的成员:
    class Entry
    {
    static void Main(string[] args)
    {
    Type t = typeof(MyClass);
    object obj = Activator.CreateInstance(t);
    ArrayList list = (ArrayList)t.InvokeMember("List",BindingFlags.GetField,null,obj,null);
    Console.WriteLine(list.Capacity);
    Console.ReadLine();
    }
    }class MyClass
    {
    public ArrayList List = new ArrayList(1024);
    }回答完毕
      

  9.   

    Eddie005(暴走005) 
       --搂主这是在研究反射,呵呵,我不过写一个示例而已然后取道ArrayList的时候,总是只取了一个Count值,并没有取道ArrayList的对象是啊,因为你的代码里面Class1 cls = new Class1();
    然后并没有给ArrayList.Add什么咚咚当然取出来ArrayList是一个count
      

  10.   

    SunmastType t = a.GetType();
    foreach(PropertyInfo p in t.GetProperties())
    {
             object obj = p.GetValue(a,null)
    }里面对象参与运算了GetValue(a,null)
    a不就是对象
      

  11.   

    通过大家的帮助,我写了一个通用的方法用来判断一个类的两个实例是否相等,现在把代码贴出来,大家看看有没有什么错误,同时也希望这段代码能对大家有用,谢谢!/// <summary>
    /// Compare if the objects are equal
    /// Notice:Since we don't use HashTable, so I don't think about hashtable 
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    private bool Compare(Object a, Object b)
    {
    if (a != null && b != null)
    {
    Type t = a.GetType();
    if (t.IsValueType || t.ToString() == "System.String")
    {
    if (Convert.ToString(a) != Convert.ToString(b))
    return false;
    else
    return true;
    } foreach(PropertyInfo p in t.GetProperties())
    {
    if (p.PropertyType.ToString() != "System.Object")
    {
    if (p.PropertyType.ToString() != "System.Collections.ArrayList")
    {
    if( Compare(p.GetValue(a,null),p.GetValue(b,null)))
    continue;
    else
    return false;
    }
    else if (p.PropertyType.ToString() == "System.Collections.ArrayList")
    {
    object ob = p.GetValue(a,null);
    if (!CompareArrayLists((ArrayList)p.GetValue(a,null) ,(ArrayList)p.GetValue(b,null)))
    return false;
    }
    }
    }
    return true;
    }
    else
    {
    if (a == null && b == null)
    return true;
    return false;
    }
    } /// <summary>
    /// ArrayList is the special way
    /// </summary>
    /// <param name="arr1"></param>
    /// <param name="arr2"></param>
    /// <returns></returns>
    private bool CompareArrayLists(ArrayList arr1,ArrayList arr2)
    {
    if(arr1.Count != arr2.Count)
    return false;

    //Iterate through each element and compare if it is equal to the corresponding element in the other
    for(int i = 0;i < arr1.Count ; i++)
    {
    if(!Compare((object)arr1[i],(object)arr2[i]))
    return false;
    } return true;
    }
      

  12.   

    看着比较乱,我重新整理了一下
    /// <summary>
    /// Compare if the objects are equal
    /// Notice:Since we don't use HashTable, so I don't think about hashtable 
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    private bool Compare(Object a, Object b)
    {
    if (a != null && b != null)
    {
    Type t = a.GetType();
    if (t.IsValueType || t.ToString() == "System.String")
    {
    if (Convert.ToString(a) != Convert.ToString(b))
    return false;
    else
    return true;
    } foreach(PropertyInfo p in t.GetProperties())
    {
    if (p.PropertyType.ToString() != "System.Object")
    {
    if (p.PropertyType.ToString() != "System.Collections.ArrayList")
    {
    if( Compare(p.GetValue(a,null),p.GetValue(b,null)))
    continue;
    else
    return false;
    }
    else if (p.PropertyType.ToString() == "System.Collections.ArrayList")
    {
    object ob = p.GetValue(a,null);
    if (!CompareArrayLists((ArrayList)p.GetValue(a,null) ,(ArrayList)p.GetValue(b,null)))
    return false;
    }
    }
    }
    return true;
    }
    else
    {
    if (a == null && b == null)
    return true;
    return false;
    }
    }/// <summary>
    /// ArrayList is the special way
    /// </summary>
    /// <param name="arr1"></param>
    /// <param name="arr2"></param>
    /// <returns></returns>
    private bool CompareArrayLists(ArrayList arr1,ArrayList arr2)
    {
    if(arr1.Count != arr2.Count)
    return false;

    //Iterate through each element and compare if it is equal to the corresponding element in the other
    for(int i = 0;i < arr1.Count ; i++)
    {
    if(!Compare((object)arr1[i],(object)arr2[i]))
    return false;
    } return true;
    }