C# 利用反射動態給模型Model 賦值


https://www.cnblogs.com/waitingfor/articles/2220669.html

 

object ff = Activator.CreateInstance(tt, null);//創建指定類型實例
 
https://www.cnblogs.com/guohu/p/4496510.html
property.SetValue(obj,Convert.ChangeType(value,property.PropertyType), null );
 
實現

            DataModel.SystemConfig systemConfig = new DataModel.SystemConfig();
            

            PropertyInfo[] ps = systemConfig.GetType().GetProperties();
            foreach (PropertyInfo p in ps)
            {
                object obj = Activator.CreateInstance(p.PropertyType);//創建指定類型實例
                PropertyInfo[] props = p.PropertyType.GetProperties();
                foreach (PropertyInfo pr in props)
                {
                    DataGridViewRow dgvr = (from item in this.rcomdgv1.Rows.Cast<DataGridViewRow>()
                                                  where item.Cells["EnName"].Value.ToString() == pr.Name
                                                  select item).FirstOrDefault();
                    pr.SetValue(obj, Convert.ChangeType(dgvr.Cells["values"].Value, pr.PropertyType),null);
                }
                p.SetValue(systemConfig, Convert.ChangeType(obj, p.PropertyType), null);
            }


免責聲明!

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



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