62,266
社区成员
发帖
与我相关
我的任务
分享
public class UserInfo
{
public Int32 ID { get; set; }
public String Name { get; set; }
}
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { ID = 1, Name = "Jerry" });
list.Add(new UserInfo() { ID = 2, Name = "Zhang" });
list.Add(new UserInfo() { ID = 3, Name = "IBM" });
list.Add(new UserInfo() { ID = 4, Name = "Microsoft" });
//对现在我如何通过反射的方法得到list里面的数据
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { ID = 1, Name = "Jerry" });
list.Add(new UserInfo() { ID = 2, Name = "Zhang" });
list.Add(new UserInfo() { ID = 3, Name = "IBM" });
list.Add(new UserInfo() { ID = 4, Name = "Microsoft" });
Type t = list.GetType();
PropertyInfo pItem = t.GetProperty("Item", BindingFlags.Public | BindingFlags.Instance);
PropertyInfo pCount = t.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance);
int count = Convert.ToInt32(pCount.GetValue(list, null));
for (int i = 0; i < count; i++)
{
object ui = pItem.GetValue(list, new object[] { i });
Type t2 = ui.GetType();
PropertyInfo pID = t2.GetProperty("ID", BindingFlags.Instance | BindingFlags.Public);
PropertyInfo pName = t2.GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
Console.WriteLine("ID:{0},Name:{1}", pID.GetValue(ui, null), pName.GetValue(ui, null));
}