如果有多个属性具有相同类型就不能用这个方法了
Test() 的输出结果为 99999 ,执行时间为 0.72 秒左右
BlankTest() 的执行时间为 0.66 秒左右
public class ManPO
{
public string Name = "StephenPO";
private LadyPO lady;
public LadyPO MyLady
{
get
{
return this.lady;
}
set
{
this.lady = value;
}
}
}
public class ManDTO
{
public string Name = "StephenDTO";
private LadyDTO lady;
public LadyDTO MyLady
{
get
{
return this.lady;
}
set
{
this.lady = value;
}
}
}
public class LadyPO
{
private int id = 11111;
public string Name = "LadyPO";
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
public class LadyDTO
{
private int id = 99999;
public string Name = "LadyDTO"
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
/// <summary>
/// OtherTest 的摘要说明。
/// </summary>
[TestFixture]
public class OtherTest
{
public OtherTest()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test]
public void BlankTest()
{
}
[Test]
public void Test()
{
// 创建关联对象映射
ArrayList relatedDTO = new ArrayList();
ArrayList relatedPO = new ArrayList();
relatedDTO.Add(typeof(ManDTO).AssemblyQualifiedName);
relatedPO.Add(typeof(ManPO).AssemblyQualifiedName);
relatedDTO.Add(typeof(LadyDTO).AssemblyQualifiedName);
relatedPO.Add(typeof(LadyPO).AssemblyQualifiedName);
// 对象初始化
LadyDTO lady = new LadyDTO();
ManDTO src = new ManDTO();
src.MyLady = lady;
ManPO target = new ManPO();
// 根据『对象所属类型』执行 级联对象属性拷贝
string targetName;
string srcName;
foreach(PropertyInfo targetProperty in target.GetType().GetProperties())
{
targetName = targetProperty.PropertyType.AssemblyQualifiedName;
if(relatedPO.Contains(targetName))
{
int i = relatedPO.IndexOf(targetName, 0);
foreach(PropertyInfo srcProperty in src.GetType().GetProperties())
{
srcName = srcProperty.PropertyType.AssemblyQualifiedName;
if(relatedDTO.Contains(srcName))
{
LadyDTO srcObj = srcProperty.GetGetMethod().Invoke(src, new object[] {}) as LadyDTO;
LadyPO targetObj = ClassLoader.forName(targetName) as LadyPO;
targetObj.Id = srcObj.Id;
// copyProperties
object[] targetValues = new object[1];
targetValues[0] = targetObj;
targetProperty.GetSetMethod().Invoke(target, targetValues);
}
}
}
}
// 输出测试结果。注意在初始化阶段 target.MyLady 没有被实例化
Console.WriteLine(target.MyLady.Id);
}
}
{
public string Name = "StephenPO";
private LadyPO lady;
public LadyPO MyLady
{
get
{
return this.lady;
}
set
{
this.lady = value;
}
}
}
public class ManDTO
{
public string Name = "StephenDTO";
private LadyDTO lady;
public LadyDTO MyLady
{
get
{
return this.lady;
}
set
{
this.lady = value;
}
}
}
public class LadyPO
{
private int id = 11111;
public string Name = "LadyPO";
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
public class LadyDTO
{
private int id = 99999;
public string Name = "LadyDTO"
public int Id
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
}
/// <summary>
/// OtherTest 的摘要说明。
/// </summary>
[TestFixture]
public class OtherTest
{
public OtherTest()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[Test]
public void BlankTest()
{
}
[Test]
public void Test()
{
// 创建关联对象映射
ArrayList relatedDTO = new ArrayList();
ArrayList relatedPO = new ArrayList();
relatedDTO.Add(typeof(ManDTO).AssemblyQualifiedName);
relatedPO.Add(typeof(ManPO).AssemblyQualifiedName);
relatedDTO.Add(typeof(LadyDTO).AssemblyQualifiedName);
relatedPO.Add(typeof(LadyPO).AssemblyQualifiedName);
// 对象初始化
LadyDTO lady = new LadyDTO();
ManDTO src = new ManDTO();
src.MyLady = lady;
ManPO target = new ManPO();
// 根据『对象所属类型』执行 级联对象属性拷贝
string targetName;
string srcName;
foreach(PropertyInfo targetProperty in target.GetType().GetProperties())
{
targetName = targetProperty.PropertyType.AssemblyQualifiedName;
if(relatedPO.Contains(targetName))
{
int i = relatedPO.IndexOf(targetName, 0);
foreach(PropertyInfo srcProperty in src.GetType().GetProperties())
{
srcName = srcProperty.PropertyType.AssemblyQualifiedName;
if(relatedDTO.Contains(srcName))
{
LadyDTO srcObj = srcProperty.GetGetMethod().Invoke(src, new object[] {}) as LadyDTO;
LadyPO targetObj = ClassLoader.forName(targetName) as LadyPO;
targetObj.Id = srcObj.Id;
// copyProperties
object[] targetValues = new object[1];
targetValues[0] = targetObj;
targetProperty.GetSetMethod().Invoke(target, targetValues);
}
}
}
}
// 输出测试结果。注意在初始化阶段 target.MyLady 没有被实例化
Console.WriteLine(target.MyLady.Id);
}
}
Test() 的输出结果为 99999 ,执行时间为 0.72 秒左右
BlankTest() 的执行时间为 0.66 秒左右