问题描述:有T1与T2类,T1类为主类,包括所有的字段,T2类只包含T1类中的部分字段。现有T1类数据,取T2类数据。
通过反射处理的方法如下:
1、方法函数
/// <summary> /// 将T1对象属性值反射到T2的对象属性上 /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="t1"></param> /// <returns></returns> public T2 T1CopyToT2<T1, T2>(T1 t1) { T2 t2 = (T2)Activator.CreateInstance(typeof(T2)); typeof(T2).GetProperties().ToList().ForEach(p => { if (p.CanWrite) { p.SetValue(t2, typeof(T1).GetProperty(p.Name).GetValue(t1)); } }); return t2; }
2、方法调用
//将ProductResultModel反射到DevRuntimeStatusModel,ProductResultModel是大类DevRuntimeStatusModel是部分类,productResult是数据 DevRuntimeStatusModel devRuntimeStatusModel = T1CopyToT2<ProductResultModel, DevRuntimeStatusModel>(productResult);
3、
T1类:ProductResultModel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model { [YSYEnitity(TableName = "电芯结果数据实体类")] [Serializable] public class ProductResultModel { ///<summary>作业员///</summary> [YSYEnitity(ColumnName = "作业员")] public String OperatorName { get; set; } ///<summary>在线数据///</summary> [YSYEnitity(ColumnName = "在线数据")] public String Online { get; set; } ///<summary>设备机台号///</summary> [YSYEnitity(ColumnName = "设备机台号")] public String EquipmentName { get; set; } ///<summary>工厂名称///</summary> [YSYEnitity(ColumnName = "工厂名称")] public String FactoryName { get; set; } ///<summary>产线///</summary> [YSYEnitity(ColumnName = "产线")] public String MfgLineName { get; set; } ///<summary>卷芯条码 ///</summary> [YSYEnitity(ColumnName = "卷芯条码 ")] public String ContainerName { get; set; } ///<summary>虚拟码 ///</summary> [YSYEnitity(ColumnName = "虚拟码 ")] public String Vcode { get; set; } ///<summary>工序名///</summary> [YSYEnitity(ColumnName = "工序名")] public String SpecName { get; set; } ///<summary>开始生产时间///</summary> [YSYEnitity(ColumnName = "开始生产时间")] public String StartTime { get; set; } ///<summary>结束时间///</summary> [YSYEnitity(ColumnName = "结束时间")] public String EndTime { get; set; } ///<summary>班次///</summary> [YSYEnitity(ColumnName = "班次")] public String WorkShit { get; set; } ///<summary>卷针号///</summary> [YSYEnitity(ColumnName ="卷针号")] public string Needle { get; set; } ///<summary>正极切刀条码///</summary> [YSYEnitity(ColumnName = "正极切刀条码")] public String CathodeKnife { get; set; } ///<summary>负极切刀条码///</summary> [YSYEnitity(ColumnName = "负极切刀条码")] public String AnodeKnife { get; set; } ///<summary>正极裁断切刀条码///</summary> [YSYEnitity(ColumnName = "正极裁断切刀条码")] public String CathodeCutKnife { get; set; } ///<summary>负极裁断切刀条码///</summary> [YSYEnitity(ColumnName = "负极裁断切刀条码")] public String AnodeCutKnife { get; set; } ///<summary>隔膜切刀条码///</summary> [YSYEnitity(ColumnName = "隔膜切刀条码")] public String DiaphragmKnife { get; set; } ///<summary>正极投入切刀次数///</summary> [YSYEnitity(ColumnName = "正极投入切刀次数")] public String CathodeKnifeCount { get; set; } ///<summary>负极投入切刀次数///</summary> [YSYEnitity(ColumnName = "负极投入切刀次数")] public String AnodeKnifeCount { get; set; } ///<summary>负极截断左备胶切刀次数///</summary> [YSYEnitity(ColumnName = "负极截断左备胶切刀次数")] public String AnodeLeftCutCount { get; set; } ///<summary>负极截断右备胶切刀次数///</summary> [YSYEnitity(ColumnName = "负极截断右备胶切刀次数")] public String AnodeRightCutCount { get; set; } ///<summary>负极截断胶切刀次数///</summary> [YSYEnitity(ColumnName = "负极截断胶切刀次数")] public String AnodeCutCount { get; set; } ///<summary>正极截断左备胶切刀次数///</summary> [YSYEnitity(ColumnName = "正极截断左备胶切刀次数")] public String CathodeLeftCutCount { get; set; } ///<summary>正极截断右备胶切刀次数///</summary> [YSYEnitity(ColumnName = "正极截断右备胶切刀次数")] public String CathodeRightCutCount { get; set; } ///<summary>正极截断胶切刀次数///</summary> [YSYEnitity(ColumnName = "正极截断胶切刀次数")] public String CathodeCutCount { get; set; } ///<summary>终止胶切刀次数///</summary> [YSYEnitity(ColumnName = "终止胶切刀次数")] public String BlueKnifeCount { get; set; } ///<summary>隔膜切刀次数///</summary> [YSYEnitity(ColumnName = "隔膜切刀次数")] public String DiaphragmKnifeCount { get; set; } ///<summary>负极极卷条码</summary> [YSYEnitity(ColumnName = "负极极卷条码")] public String AnodeRollContainer { get; set; } = ""; ///<summary>正极极卷条码</summary> [YSYEnitity(ColumnName = "正极极卷条码")] public String CathodeRollContainer { get; set; } = ""; ///<summary>收尾胶条码</summary> [YSYEnitity(ColumnName = "收尾胶条码")] public String TapeContainer { get; set; } = ""; ///<summary>高温胶条码</summary> [YSYEnitity(ColumnName = "高温胶条码")] public String GreenTape { get; set; } = ""; ///<summary>上隔膜条码</summary> [YSYEnitity(ColumnName = "上隔膜条码")] public String UpDiaphragmContainer { get; set; } = ""; ///<summary>下隔膜条码</summary> [YSYEnitity(ColumnName = "下隔膜条码")] public String DownDiaphragmContainer { get; set; } = ""; ///<summary>正极极耳总宽度///</summary> [YSYEnitity(ColumnName = "正极极耳总宽度")] public String CathodeTabWidth { get; set; } ///<summary>负极极耳总宽度///</summary> [YSYEnitity(ColumnName = "负极极耳总宽度")] public String AnodeTabWidth { get; set; } ///<summary>首片极耳边缘侧至卷芯边沿短边距离(偏心侧)///</summary> [YSYEnitity(ColumnName = "首片极耳边缘侧至卷芯边沿短边距离(偏心侧)")] public String FirstPieceOffCentreLenth { get; set; } ///<summary>卷绕负极极片计长///</summary> [YSYEnitity(ColumnName = "卷绕负极极片计长")] public String AnodeElectrodeLength { get; set; } ///<summary>卷绕正极极片计长///</summary> [YSYEnitity(ColumnName = "卷绕正极极片计长")] public String CathodeElectrodeLength { get; set; } ///<summary>正极长度设定///</summary> [YSYEnitity(ColumnName = "正极长度设定")] public String CathodeLengthSetting { get; set; } ///<summary>负极长度设定///</summary> [YSYEnitity(ColumnName = "负极长度设定")] public String AnodeLengthSetting { get; set; } ///<summary>隔膜到负极Overhang(卷芯头部)最大值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯头部)最大值")] public String DiaAnOverhangHeadMax { get; set; } ///<summary>隔膜到负极Overhang(卷芯头部)最小值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯头部)最小值")] public String DiaAnOverhangHeadMin { get; set; } ///<summary>隔膜到负极Overhang(卷芯头部)设定值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯头部)设定值")] public String DiaAnOverhangHeadSetting { get; set; } ///<summary>隔膜到负极Overhang(卷芯头部)判定值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯头部)判定值")] public String DiaAnOverhangHeadIsOk { get; set; } ///<summary>隔膜到负极Overhang(卷芯尾部)最大值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯尾部)最大值")] public String DiaAnOverhangTailMax { get; set; } ///<summary>隔膜到负极Overhang(卷芯尾部)最小值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯尾部)最小值")] public String DiaAnOverhangTailMin { get; set; } ///<summary>隔膜到负极Overhang(卷芯尾部)设定值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯尾部)设定值")] public String DiaAnOverhangTailSetting { get; set; } ///<summary>隔膜到负极Overhang(卷芯尾部)判定值///</summary> [YSYEnitity(ColumnName = "隔膜到负极Overhang(卷芯尾部)判定值")] public String DiaAnOverhangTailIsOk { get; set; } ///<summary>隔膜到正极Overhang(卷芯头部)最大值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯头部)最大值")] public String DiaCaOverhangHeadMax { get; set; } ///<summary>隔膜到正极Overhang(卷芯头部)最小值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯头部)最小值")] public String DiaCaOverhangHeadMin { get; set; } ///<summary>隔膜到正极Overhang(卷芯头部)设定值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯头部)设定值")] public String DiaCaOverhangHeadSetting { get; set; } ///<summary>隔膜到正极Overhang(卷芯头部)判定值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯头部)判定值")] public String DiaCaOverhangHeadOk { get; set; } ///<summary>隔膜到正极Overhang(卷芯尾部)最大值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯尾部)最大值")] public String DiaCaOverhangTailMax { get; set; } ///<summary>隔膜到正极Overhang(卷芯尾部)最小值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯尾部)最小值")] public String DiaCaOverhangTailMin { get; set; } ///<summary>隔膜到正极Overhang(卷芯尾部)设定值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯尾部)设定值")] public String DiaCaOverhangTailSetting { get; set; } ///<summary>隔膜到正极Overhang(卷芯尾部)判定值///</summary> [YSYEnitity(ColumnName = "隔膜到正极Overhang(卷芯尾部)判定值")] public String DiaCaOverhangTailOk { get; set; } ///<summary>负极到正极Overhang(卷芯头部)最大值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯头部)最大值")] public String AnCaOverhangHeadMax { get; set; } ///<summary>负极到正极Overhang(卷芯头部)最小值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯头部)最小值")] public String AnCaOverhangHeadMin { get; set; } ///<summary>负极到正极Overhang(卷芯头部)设定值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯头部)设定值")] public String AnCaOverhangHeadSetting { get; set; } ///<summary>负极到正极Overhang(卷芯头部)判定值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯头部)判定值")] public String AnCaOverhangHeadIsOk { get; set; } ///<summary>负极到正极Overhang(卷芯尾部)最大值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯尾部)最大值")] public String AnCaOverhangTailMax { get; set; } ///<summary>负极到正极Overhang(卷芯尾部)最小值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯尾部)最小值")] public String AnCaOverhangTailMin { get; set; } ///<summary>负极到正极Overhang(卷芯尾部)设定值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯尾部)设定值")] public String AnCaOverhangTailSetting { get; set; } ///<summary>负极到正极Overhang(卷芯尾部)判定值///</summary> [YSYEnitity(ColumnName = "负极到正极Overhang(卷芯尾部)判定值")] public String AnCaOverhangTailIsOk { get; set; } ///<summary>负极到AT9 Overhang(卷芯头部)最大值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯头部)最大值")] public String AnAt9OverhangHeadMax { get; set; } ///<summary>负极到AT9 Overhang(卷芯头部)最小值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯头部)最小值")] public String AnAt9OverhangHeadMin { get; set; } ///<summary>负极到AT9 Overhang(卷芯头部)设定值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯头部)设定值")] public String AnAt9OverhangHeadSetting { get; set; } ///<summary>负极到AT9 Overhang(卷芯头部)判断值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯头部)判断值")] public String AnAt9OverhangHeadIsOk { get; set; } ///<summary>负极到AT9 Overhang(卷芯尾部)最大值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯尾部)最大值")] public String AnAt9OverhangTailMax { get; set; } ///<summary>负极到AT9 Overhang(卷芯尾部)最小值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯尾部)最小值")] public String AnAt9OverhangTailMin { get; set; } ///<summary>负极到AT9 Overhang(卷芯尾部)设定值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯尾部)设定值")] public String AnAT9OverhangTailSetting { get; set; } ///<summary>负极到AT9 Overhang(卷芯尾部)判定值///</summary> [YSYEnitity(ColumnName = "负极到AT9 Overhang(卷芯尾部)判定值")] public String AnAt9OverhangTailIsOk { get; set; } ///<summary>正极耳翻折宽度最小值///</summary> [YSYEnitity(ColumnName = "正极耳翻折宽度最小值")] public String CaTabFoldMinWidth { get; set; } ///<summary>负极耳翻折宽度最小值///</summary> [YSYEnitity(ColumnName = "负极耳翻折宽度最小值")] public String AnTabFoldMinWidth { get; set; } ///<summary>正极耳翻折数量///</summary> [YSYEnitity(ColumnName = "正极耳翻折数量")] public String CaTabFoldNum { get; set; } ///<summary>负极耳翻折数量///</summary> [YSYEnitity(ColumnName = "负极耳翻折数量")] public String AnTabFoldNum { get; set; } ///<summary>正极耳翻折宽度设定值///</summary> [YSYEnitity(ColumnName = "正极耳翻折宽度设定值")] public String CaFoldSettingWidth { get; set; } ///<summary>负极耳翻折宽度设定值///</summary> [YSYEnitity(ColumnName = "负极耳翻折宽度设定值")] public String AnFoldSettingWidth { get; set; } ///<summary>材料 B/N///</summary> [YSYEnitity(ColumnName = "材料 B/N")] public String BnName { get; set; } ///<summary>参数判定结果///</summary> [YSYEnitity(ColumnName = "参数判定结果")] public String Success { get; set; } ///<summary>不良代码///</summary> [YSYEnitity(ColumnName = "不良代码")] public String LossReason { get; set; } ///<summary>正极极耳错位///</summary> [YSYEnitity(ColumnName = "正极极耳错位")] public String CaDislocation { get; set; } ///<summary>负极极耳错位///</summary> [YSYEnitity(ColumnName = "负极极耳错位")] public String AnDislocation { get; set; } ///<summary>CCD正极到隔膜距离///</summary> [YSYEnitity(ColumnName = "CCD正极到隔膜距离")] public String DisAnDiaphragm { get; set; } ///<summary>CCD负极到隔膜距离///</summary> [YSYEnitity(ColumnName = "CCD负极到隔膜距离")] public String DisCaDiaphragm { get; set; } ///<summary>CCD正极到负极距离///</summary> [YSYEnitity(ColumnName = "CCD正极到负极距离")] public String DisCaAn { get; set; } ///<summary>创建时间///</summary> [YSYEnitity(ColumnName = "创建时间")] public String CreateTime { get; set; } } }View Code
T2类:DevRuntimeStatusModel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Model { [YSYEnitity(TableName= "设备实时状态实体类")] [Serializable] public class DevRuntimeStatusModel { /// <summary> /// 读取时间 /// </summary> [YSYEnitity(ColumnName = "读取时间")] public string ReadTime { get; set; } /// <summary> /// 员工工号 /// </summary> [YSYEnitity(ColumnName = "员工工号")] public string Operator { get; set; } /// <summary> /// 员工姓名 /// </summary> [YSYEnitity(ColumnName = "员工姓名")] public string EmployeeName { get; set; } /// <summary> /// 设备编号 /// </summary> [YSYEnitity(ColumnName = "设备编号")] public string DevSerialNO { get; set; } /// <summary> /// 卷芯型号 /// </summary> [YSYEnitity(ColumnName = "卷芯型号")] public string DevProType { get; set; } /// <summary> /// 负极片张力 /// </summary> [YSYEnitity(ColumnName = "负极片张力")] public double TensionOfNegative { get; set; } /// <summary> /// 上隔膜张力 /// </summary> [YSYEnitity(ColumnName = "上隔膜张力")] public double TensionOfUpGeMo { get; set; } /// <summary> /// 下隔膜张力 /// </summary> [YSYEnitity(ColumnName = "下隔膜张力")] public double TensionOfDownGeMo { get; set; } /// <summary> /// 正极片张力 /// </summary> [YSYEnitity(ColumnName = "正极片张力")] public double TensionOfPositive { get; set; } } }View Code
productResult为productResultModel类型对象,由数据采集后赋值。devRuntimeStatusModel 是通过反射,从productResult中把自己对象的属性字段值拿出来。