string strRequestValue = null; 
   PropertyInfo[] proInfo = Modules.GetType().GetProperties();
   foreach (PropertyInfo p in proInfo)
   {
           if (p.PropertyType.IsClass)
           {
                    PropertyInfo[] childProInfo = p.PropertyType.GetProperties();
                    foreach (PropertyInfo item in childProInfo)
                    {
                        strRequestValue = Request[p.Name + "." + item.Name];
                        if (strRequestValue != null)
                        {
                            item.SetValue(p, GetValue(strRequestValue, item), null);
                        }
                    }
            }
            …………
  }    
上面代码中Modules表示当前要赋值的实体类对象,比如这样一个实体类
   public class EntityArticle
    {
        private EntityNode _node;
        public EntityNode node
        {
            get { return _node; }
            set { _node = value; }
        }
        private EntityNodeAttach _nodeAttach;
        public EntityNodeAttach nodeAttach
        {
            get{return _nodeAttach;}
            set{_nodeAttach = value;}
        }
        ……
    }
问题就出在:item.SetValue(p, GetValue(strRequestValue, item), null);
SetValue的第一个参数需要“设置其属性值得对象”,但是我不知道在这里如何得到这个属性对象
希望能有人指点一下,谢谢!
                    
                    
                6 回答
 
                    
                    
                            莫回无
                            
                        
                                                
                    TA贡献1865条经验 获得超7个赞
                                关键代码在这,亲测可用,如果你还不行,联系我找我要测试代码
 
if (p.PropertyType.IsClass){    var type1 = p.PropertyType;    var obj = Assembly.Load(type1.Assembly.FullName).CreateInstance(type1.FullName);    var properties1 = type1.GetProperties();    foreach (var p1 in properties1)    {        p1.SetValue(obj, "111", null);    }    p.SetValue(t, obj, null);} 
                            
                            
                        - 6 回答
- 0 关注
- 980 浏览
添加回答
举报
0/150
	提交
		取消
	
 
                     
                     
                    