1.定义一个方法
private T CreateNewValue<T>(T oldobj, Func<string, object, object> handersub)
{
var typeinfo = typeof(T);
var list = new List<object>();
var ps = typeinfo.GetProperties();
var chg = false;
foreach (var prop in ps) {
var oldv = prop.GetValue(oldobj);
var newv = handersub(prop.Name, oldv);
list.Add(newv);
chg = chg || (oldv != newv);
}
if (!chg) { return oldobj; }
return (T)Activator.CreateInstance(typeinfo, list.ToArray());
}
2.应用:
j = CreateNewValue(j, (name, oldv) =>
{
if (name == "ExecutionProgress")
{
return ExecutionProgress;
}
if (name == "ProjectPhaseStatus")
{
return ProjectPhaseStatus;
}
return oldv;
});