我需要從嵌套屬性中獲取值。我需要從嵌套屬性中獲取值
在MainClass我有一個學生屬性是學生類的類型。
MainClass obj = new MainClass();
obj.Name = "XII";
obj.Students = new List<Students>()
{
new Students() { ID = "a0", Name = "A" },
new Bridge() { ID = "a1", Name = "B" }
};
Type t = Type.GetType(obj.ToString());
PropertyInfo p = t.GetProperty("Students");
object gg = p.GetValue(obj, null);
var hh = gg.GetType();
string propertyType = string.Empty;
if (hh.IsGenericType)
{
string[] propertyTypes = hh.ToString().Split('[');
Type gggg = Type.GetType(propertyTypes[1].Remove(propertyTypes[1].Length - 1));
foreach (PropertyInfo pro in gggg.GetProperties())
{
if (pro.Name.Equals("Name"))
{
// Get the value here
}
}
}
爲什麼不使用List的'Find'方法? – V4Vendetta 2011-05-09 06:49:20
你究竟在找什麼?是真的需要反思嗎?什麼是「Bridge」類? – SWeko 2011-05-09 06:52:10