复制数据:
PropertyInfo[] props = typeof(Tb_ChangedResult).GetProperties(BindingFlags.Public | BindingFlags.Instance);
Tb_ChangedResult newmodel = new Tb_ChangedResult();
newmodel.ID = Guid.NewGuid().ToString("D").ToUpper();
newmodel.TestApplyBuildingID = stringar[i];
newmodel.BuildingID = strbuild[i];
for (int j = 0; j < props.Length; j++)
{
if (props[j].Name != "ID" && props[j].Name != "TestApplyBuildingID" && props[j].Name != "BuildingID")//去除之前赋值项
{
MethodInfo setm = props[j].SetMethod; //考虑到扩展属性的问题,一般扩展属性没有set{},只有get{}
if (setm != null) //若是不为空,则是类本身的属性,并非扩展类属性
props[j].SetValue(newmodel, props[j].GetValue(model, null));//获取model的属性值赋值给newmodel
}
}
db.Tb_ChangedResult.Add(newmodel);
主要用于:循环添加数据,其中只有几项属性值不同,其他属性可以很据这样的方法获取赋值