C# 遍历对象下的 属性
foreach (System.Reflection.PropertyInfo p in users.GetType().GetProperties()) { var xx = p.Name; var yy = p.GetValue(users); }
Type type = typeof(pof); //这里是类名 Pof pf = new Pof();
System.Reflection.PropertyInfo pi = type.GetProperty("B"); var ss =pi.GetValue(pf,null);
pi.SetValue(pf,"111",null);
foreach (PropertyInfo p in pf.GetType().GetProperties()) { var xx = p.Name; var yy = p.GetValue(pf,null);
p.SetValue(pf,"111",null); }
string[] Noviod = {"","",""}; foreach (System.Reflection.PropertyInfo p in model.GetType().GetProperties()) { for (int i=0;i<Noviod.Length;i++) { if (p.Name == Noviod[i]) { if (p.GetValue(model).ToString()=="") { return Content("数据错误!"); } } } }
List<model> list = new List<model>(); for (int i = 0; i < 10; i++) { model md = new model(); md.A = "AAA" + i; md.B = "BBB" + i; list.Add(md); } //每一行的数据 是AAA,BBB ; A和B表示列名 //foreach (PropertyInfo p in list[0].GetType().GetProperties()) //{ // var xx = p.Name; //属性名, // var yy = p.GetValue(p, null); //属性值 //} model mode = new model(); var models = mode.GetType().GetProperties(); for (int i = 0; i < models.Length; i++) { var name = models[i].Name; if (name == "A") { continue; } var value = models[i].GetValue(mode); } for (int i = 0; i < list.Count; i++) { var modeps = list[i].GetType().GetProperties(); //获得该对象所有属性名 for (int ii = 0; ii < modeps.Length; ii++) { var name = modeps[ii].Name; //获得属性名 if (name == "A") { continue; } var value = modeps[ii].GetValue(list[i]); //获得属性值 } } MessageBox.Show("完成!");
A a = new A(); a.A_01 = "123"; a.A_02 = "321"; a.A_03 = "000"; foreach (System.Reflection.PropertyInfo p in a.GetType().GetProperties()) { if (p.Name == "A_03") //遍历属性 { continue; } p.SetValue(a, null, null); }