1. 程式人生 > >資料庫有很多欄位,C#通過反射輕鬆GET/SET

資料庫有很多欄位,C#通過反射輕鬆GET/SET

using (var dbcontext = new dbautotest4in1Entities1())
            {
                var hts  =  dbcontext.hardwaretable.Where(h=>h.hwid  ==  "9500");
                hardwaretable bt = hts.First();
                Type type = bt.GetType();
                List<PropertyInfo> ps = type.GetProperties().ToList();
                for(int i =0; i<ps.Count;i++)
                {
                    if(ps[i].Name.Contains("2018"))
                    {
                        PropertyInfo pi = ps[i];
                        listBox1.Items.Add(pi.GetValue(bt));
                        //pi.SetValue(bt, 10f);
                        //dbcontext.SaveChanges();
                    }
                }