我所擁有的是對象的ArrayList,我試圖使用反射來獲取ArrayList中每個對象的每個屬性的名稱。例如:使用反射來查找ArrayList對象屬性
private class TestClass
{
private int m_IntProp;
private string m_StrProp;
public string StrProp
{
get
{
return m_StrProp;
}
set
{
m_StrProp = value;
}
}
public int IntProp
{
get
{
return m_IntProp;
}
set
{
m_IntProp = value;
}
}
}
ArrayList al = new ArrayList();
TestClass tc1 = new TestClass();
TestClass tc2 = new TestClass();
tc1.IntProp = 5;
tc1.StrProp = "Test 1";
tc2.IntProp = 10;
tc2.StrPRop = "Test 2";
al.Add(tc1);
al.Add(tc2);
foreach (object obj in al)
{
// Here is where I need help
// I need to be able to read the properties
// StrProp and IntProp. Keep in mind that
// this ArrayList may not always contain
// TestClass objects. It can be any object,
// which is why I think I need to use reflection.
}
你使用一個ArrayList,而不是一個[清單(http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx)的原因嗎? –
dtb
@dtb - 是的,因爲我的列表可能不一定是TestClass。它可以容納任何物體。 – Icemanind
'列表