我的方法如下: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);
}
}
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);
}
}
解决方案 »
- C#多线程中ShowDialog()方法无效
- 如何设置winform中listview控件中的checkbox的值为true
- 从OleDb读取Excel,非空值从Excel读出来的数据变成了空。请问该怎么处理
- 急~如何从datagridview中把被选中CheckBox的多行存入到LIST中?
- 小妹请教前辈数字格式判断的正则表达式!
- 2007-06-22T18:02:46.578125+08:00 这样的时间格式如何获得?
- 看了这篇文章,我觉得学编程也是不能死抠语法,高手们觉得是不是?
- 什么样的场合适用静态字段和静态方法?
- 有没有更好的取Win DataGrid行、列的方法
- 关于srting一个的函数的问题!!!
- 操作powerpoint怎么编译通不过啊,原文按微软网站做的啊. 请帮我啊!
- 对于把更新后的结果存储到数据库里很迷茫
ArrayList al = one.GetAL;
{
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的对象
class MyClass
{
public ArrayList List;
}
要得到List属性可以这么写:
Type t = typeof(MyClass);
MemberInfo[] members = t.GetMember("List");
Console.WriteLine(members[0].Name);
...但是不知道你的意图究竟如何
foreach(PropertyInfo p in t.GetProperties())
{
object obj = p.GetValue(a,null)
}////////////////////////////////////////你这么写当然得不到了
对象都没有参与运算,如何得到实际内容?
为什么不直接使用cls.xxx来取得呢,而非得使用反射
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;}
}
}
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);
}回答完毕
--搂主这是在研究反射,呵呵,我不过写一个示例而已然后取道ArrayList的时候,总是只取了一个Count值,并没有取道ArrayList的对象是啊,因为你的代码里面Class1 cls = new Class1();
然后并没有给ArrayList.Add什么咚咚当然取出来ArrayList是一个count
foreach(PropertyInfo p in t.GetProperties())
{
object obj = p.GetValue(a,null)
}里面对象参与运算了GetValue(a,null)
a不就是对象
/// 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;
}
/// <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;
}