for (int k = 1; k < 20; k++) { string value = "SubjectMoney" + k; System.Reflection.PropertyInfo[] propertys = teaList[i].GetType().GetProperties(); TeaAns.Append(propertys.Where(m => m.Name == value).FirstOrDefault().GetValue(teaList[i])); TeaAns.Append("♂"); }
或者
System.Reflection.PropertyInfo[] propertys = teaList[i].GetType().GetProperties(); foreach (System.Reflection.PropertyInfo pinfo in propertys) { for (int k = 1; k < 20; k++) { string value = "SubjectMoney" + k; if (value == pinfo.Name) { pinfo.GetValue(teaList[i], null); } } }
for (int k = 1; k < 20; k++) { string value = "SubjectMoney" + k; System.Reflection.PropertyInfo[] propertys = teaList[i].GetType().GetProperties(); TeaAns.Append(propertys.Where(m => m.Name == value).FirstOrDefault().GetValue(teaList[i])); TeaAns.Append("♂"); }