创建新的实体copy--反射,以免引用类型赋值覆盖遇到问题!急求高手解答!

dicklee1214 2009-11-24 12:07:13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snphk.App.Common;
using AppCommon;
using Snphk.App.DDM.BasicInfo;
using System.Runtime.Serialization;
using System.Reflection;

namespace Snphk.App.DDM.AcceptHailOil
{
public partial class DDM_HairOilLine : IValid, IEntity
{
public DDM_HairOilLine GetEntityClone()
{
DDM_HairOilLine rtnHairOilLine = new DDM_HairOilLine();
PropertyInfo[] entityPropertiesCopy = rtnHairOilLine.GetType().GetProperties();
PropertyInfo[] entityProperties = this.GetType().GetProperties();
foreach (PropertyInfo pi in entityPropertiesCopy)
{
for (int i = 0; i < entityProperties.Length; i++)
{
if (pi.Name == entityProperties[i].Name)
{
pi.SetValue(null, entityProperties[i].GetValue(this, null), null);
break;
}
}
}
return rtnHairOilLine; }
}
}

这一句总出错pi.SetValue(null, entityProperties[i].GetValue(this, null), null);
“报错:System.ArgumentException: 未找到属性设置方法。”
调用方式:hoCopy = hairOilLineTemp.GetEntityClone();
hoCopy 为未赋值DDM_HairOilLine类型 对象,hairOilLineTemp为源
DDM_HairOilLine类型 对像
-----------------请问这个问题是怎么回事,该怎么解决!
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wartim 2009-11-24
  • 打赏
  • 举报
回复
pi.SetValue(null, entityProperties[i].GetValue(this, null), null);

object=null?那你想给哪个对象设置这个属性啊
我姓区不姓区 2009-11-24
  • 打赏
  • 举报
回复
try:
pi.SetValue(rtnHairOilLine, entityProperties[i].GetValue(this, null), null);
zhujiazhi 2009-11-24
  • 打赏
  • 举报
回复
pi.SetValue(rtnHairOilLine, entityProperties[i].GetValue(this, null), null);
showlin 2009-11-24
  • 打赏
  • 举报
回复
该属性没有set访问器
zl194 2009-11-24
  • 打赏
  • 举报
回复
GZ
wuyq11 2009-11-24
  • 打赏
  • 举报
回复
propertyInfo.SetValue(目标对象,entityProperties[i].GetValue(this, null), null);

propertyInfo.SetValue(rtnHairOilLine ,entityProperties[i].GetValue(this, null), null);

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧