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);
            }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM