.net 父类值赋给子类
2024-09-06 18:58:27
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;
}
最新文章
- Oracle:试图访问正在使用的事务临时表
- android 通过WiFi进行adb调试
- Android之手机向导以及设置中心模块的开发
- CF456D A Lot of Games (字典树+DP)
- Mac安装wget的两种方法
- 开源 免费 java CMS - FreeCMS1.5-系统配置
- #include <;boost/thread.hpp>;
- 02-TypeScript中新的字符串
- ACM学习之路___HDU 2066 一个人的旅行
- HTML中的Hack条件注释语句
- 寒冬之下,移动开发没人要了? 浅谈 iOS 开发者该 何去何从?
- Pytorch 报错总结
- jooq实践
- Linux.Centos.yum命令的“No module named yum”错误
- 01 Linux简介
- 程序员,不要让自己做兔子(updated) 网上最近流传的一个笑话,关于兔子,狼还有一只老虎的,故事 我就是想打你了,还需要什么理由吗?谁让你是兔子 项目经理是这样当的
- 05: python中的函数
- HTML解析模块
- YourKit Java Profiler安装和破解
- np.array()和np.mat()区别
热门文章
- js原型和原型链的简单理解
- 用上自己的线程池,实现自己的RPC框架
- LUA解析json小demo
- 家用联通光纤开启IPv6
- python UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0x99 in position 87: illegal multibyte sequence异常解决
- 机器学习(ML)九之GRU、LSTM、深度神经网络、双向循环神经网络
- CCF_201503-2_数字排序
- Codeforces 1011C Fly(二分+模拟)
- 题解 SP375 【QTREE - Query on a tree】
- 协程的原理(Coroutine Theory)