我有这个类的两个对象:
public class A
{
public int Id { get; set; }
public string Name { get; set; }
public IList<B> Bs { get; set; }
}
public class B
{
public string Description { get; set; }
public IList<C> Cs { get; set; }
}
public class C
{
public string Description { get; set; }
}
现在我想用 FastMember 遍历两个对象实例的所有属性,并将属性从 复制object 1
到object 2
.
这是我到目前为止得到的(不工作!)
// Set accessors
var sourceAccessor = ObjectAccessor.Create(object1);
var targetAccessor = ObjectAccessor.Create(object2);
foreach (var sourceItem in (IList)sourceAccessor.Target)
{
var targetItemAccessor = ObjectAccessor.Create(targetAccessor.Target);
var sourceItemAccessor = ObjectAccessor.Create(sourceItem);
// Overwrite property
targetItemAccessor[p] = sourceItemAccessor[p];
}
这段代码显然不起作用......有什么想法吗?谢谢!