1、最简单的方式,反射+泛型

优点:字段修改时,无需更改代码,只需要更新实体即可

缺点:因为用到反射,可能效率会稍微弱那么一点点,没有实际用太多字段测试

public static cClass ParentCopyToChild<pClass,cClass>(pClass parent) where cClass: class,pClass,new()
{
//ChildClass child = new ChildClass();
cClass child = new cClass(); var ParentType = typeof(pClass); var Properties = ParentType.GetProperties(); foreach (var Propertie in Properties)
{
if (Propertie.CanRead && Propertie.CanWrite)
{
Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
}
} return child;
}

2、子类中加入赋值操作

public class TreeViewOrgEx:TreeViewOrg
{
public long ParentCode = 0;
public string TypeName = string.Empty; //树开启多选框时,此属性决定是否默认选中
public bool isChecked;
//树开启多选框时,此属性决定多选框是否禁用
public bool chkDisabled;
//树是否默认展开
public bool open = true; public int ISRole = 0; //需要一个默认构造函数
public TreeViewOrgEx()
{ } public TreeViewOrgEx(TreeViewOrg orgBase)
{
this.ID = orgBase.ID;
this.OrgCode = orgBase.OrgCode;
this.OrgName = orgBase.OrgName;
this.OrgLevel = orgBase.OrgLevel;
this.OrgType = orgBase.OrgType;
this.SourceCode = orgBase.SourceCode;
this.UpdateTime = orgBase.UpdateTime;
}
}

  赋值

TreeViewOrg tempOrg = new TreeViewOrg();

tempOrg.OrgCode = 10000000000;

TreeViewOrgEx orgEx = new TreeViewOrgEx(tempOrg);

orgEx.ParentCode = 20000000000;

  3、写方法操作

public void TreeViewOrgToTreeViewOrgEx(TreeViewOrg org, TreeViewOrgEx orgEx)
{
orgEx.ID = org.ID;
orgEx.OrgCode = org.OrgCode;
orgEx.OrgName = org.OrgName;
orgEx.OrgLevel = org.OrgLevel;
orgEx.OrgType = org.OrgType;
orgEx.SourceCode = org.SourceCode;
orgEx.UpdateTime = org.UpdateTime;
}

  

最新文章

  1. Oracle:试图访问正在使用的事务临时表
  2. android 通过WiFi进行adb调试
  3. Android之手机向导以及设置中心模块的开发
  4. CF456D A Lot of Games (字典树+DP)
  5. Mac安装wget的两种方法
  6. 开源 免费 java CMS - FreeCMS1.5-系统配置
  7. #include &lt;boost/thread.hpp&gt;
  8. 02-TypeScript中新的字符串
  9. ACM学习之路___HDU 2066 一个人的旅行
  10. HTML中的Hack条件注释语句
  11. 寒冬之下,移动开发没人要了? 浅谈 iOS 开发者该 何去何从?
  12. Pytorch 报错总结
  13. jooq实践
  14. Linux.Centos.yum命令的“No module named yum”错误
  15. 01 Linux简介
  16. 程序员,不要让自己做兔子(updated) 网上最近流传的一个笑话,关于兔子,狼还有一只老虎的,故事 我就是想打你了,还需要什么理由吗?谁让你是兔子 项目经理是这样当的
  17. 05: python中的函数
  18. HTML解析模块
  19. YourKit Java Profiler安装和破解
  20. np.array()和np.mat()区别

热门文章

  1. js原型和原型链的简单理解
  2. 用上自己的线程池,实现自己的RPC框架
  3. LUA解析json小demo
  4. 家用联通光纤开启IPv6
  5. python UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0x99 in position 87: illegal multibyte sequence异常解决
  6. 机器学习(ML)九之GRU、LSTM、深度神经网络、双向循环神经网络
  7. CCF_201503-2_数字排序
  8. Codeforces 1011C Fly(二分+模拟)
  9. 题解 SP375 【QTREE - Query on a tree】
  10. 协程的原理(Coroutine Theory)