我打算做个后台赋值函数,主要思路是将model的属性名和container中的每个控件名进行比对,相同则赋值。但好像反射的开销比较大,我想请问一下如下这段代码段效率是否可行?
SetValue(Container c, object o){
PropertyInfo[] props = model.GetType().GetProperties();
foreach(Componet cmp in c.Items){
if(cmp is Container)SetValue(c, o, props);
else {find PropertyInfo pi... cmp.SetValue(pi.GetValue(o, null));}
}
}
SetValue(Container c, object o, PropertyInfo[] props){
foreach(Componet cmp in c.Items){
if(cmp is Container)SetValue(c, o, props);
else {find PropertyInfo pi... cmp.SetValue(pi.GetValue(o, null));}
}
}
我现在就担心的是pi.GetValue(Model,null)这个方法的开销会不会比较大,这样递归起来就恐怖了。