C#反射

编程遇到一个问题四个类字段都是三十多个,其中有十多个是相同的,需要将一个类的字段赋值给另外一个类,开始是一个个赋值,写了几百行,后来同事提醒用反射

例子1:

 1 public IEnumerable<MaintenanceRecord> getPMMaintenanceRecord2()    //简化写法
 2         {
 3             BaseManager bm = new BaseManager();
 4             var db = bm.GetDbContext();
 5             var qq = (from t in db.MaintenanceRecords
 6                       join b in db.EquipMasters on t.EquipID equals b.EquipID
 7                       join c in             //取得这张单的按周期或者按频率的数据
 8                           (from a in db.PMSchedules.Where(b => b.MOrder != "")
 9                            join b in
10                                (from u in db.PMProcedures group u by new { PMId = u.PMId } into g select new { g.Key.PMId, MaintenanceCycleUnit = g.Max(p => p.MaintenanceCycleUnit), monthofDay = g.Max(p => p.monthofDay) })
11                            on a.PMId equals b.PMId
12                            select new { MOrder = a.MOrder, PMId = b.PMId, MaintenanceCycleUnit = b.MaintenanceCycleUnit, monthofDay = b.monthofDay })
13                             on t.MOrder equals c.MOrder
14                       join d in             //取得这张单的按周期或者按频率的数据
15                           (from u in db.PMServicings group u by new { MOrder = u.MOrder } into g select new { g.Key.MOrder, Result = g.Min(p => (p.Result == null ? "" : p.Result)), CMReason = g.Max(p => p.CMReason), CMDetailReason = g.Max(p => p.CMDetailReason), CMECDate = g.Max(p => p.CMECDate) })
16                       on t.MOrder equals d.MOrder
17                       into tb1
18                       from d in tb1.DefaultIfEmpty()  //Linq 左连接 left join
19                                                       //join c in db.PMSchedules on t.MOrder equals c.MOrder
20                                                       ////join d in db.PMProcedures.GroupBy(p => new { p.PMId, p.MaintenanceCycleUnit }).Select(s => new { sub = s.Key, avg = s.Average(a => a.score) }) 
21                                                       ////join d in
22                                                       ////    (from d in db.PMProcedures
23                                                       ////    group d by new { d.PMId, d.MaintenanceCycleUnit } into g
24                                                       ////     select g)
25                                                       ////on c.PMId equals d
26                                                       //join d in db.PMProcedures on c.PMId equals d.PMId
27                                                       //where string.IsNullOrEmpty(InnerModel_Plant) ? true : t.Plant.Contains(InnerModel_Plant)  //这种写法在wc和buinding都不为空的时候有问题,没找到原因。
28                                                       //&& string.IsNullOrEmpty(MainWC) ? true : b.MainWC.Contains(MainWC)
29                                                       //&& string.IsNullOrEmpty(InnerModel_EquipID) ? true : t.EquipID == InnerModel_EquipID
30                                                       //&& string.IsNullOrEmpty(InnerModel_PlanDateStart) ? true : t.PlanDate > Convert.ToDateTime(InnerModel_PlanDateStart)
31                                                       //&& string.IsNullOrEmpty(InnerModel_PlanDateEnd) ? true : t.PlanDate > Convert.ToDateTime(InnerModel_PlanDateEnd) 
32                       select new
33                       {
34                           MOrder = t.MOrder
35                            ,
36                           Plant = t.Plant
37                         ,
38                           Area = c.MaintenanceCycleUnit   //t.Area    //显示保养类型--月季年
39                            ,
40                           PartGroup = c.monthofDay   //显示保养类型--每月的第几天
41                            ,
42                           
43                           CMReason = d.CMReason
44                 ,
45                           CMDetailReason = d.CMDetailReason
46                 ,
47                           CMECDate = d.CMECDate
48                 ,
49                           remark = (string.IsNullOrEmpty(b.Remark) ? "" : " (" + b.Remark + ")")
50                       }).ToList();
51 
52             PropertyInfo[] infos = qq.ToList().First().GetType().GetProperties();
53             PropertyInfo[] modelinfos = typeof(MaintenanceRecord).GetProperties();
54             List<MaintenanceRecord> list = new List<MaintenanceRecord>();
55             foreach (var obj in qq)
56             {
57                 MaintenanceRecord model = new MaintenanceRecord();
58                 foreach (var item in modelinfos)
59                 {
60                     PropertyInfo pitem = infos.Where(o => o.Name == item.Name).FirstOrDefault();
61                     if(pitem!=null)
62                         item.SetValue(model, pitem.GetValue(obj));
63                 }
64                 list.Add(model);
65             }
66             return list;
67         }
View Code

例子2:

 1  public IEnumerable<MaintenanceRecord> getPMMaintenanceRecord3()     //简化写法
 2         {
 3             BaseManager bm = new BaseManager();
 4             var db = bm.GetDbContext();
 5             var qq = from t in db.MaintenanceRecords
 6                       join b in db.EquipMasters on t.EquipID equals b.EquipID
 7                       join c in             //取得这张单的按周期或者按频率的数据
 8                           (from a in db.PMSchedules.Where(b => b.MOrder != "")
 9                            join b in
10                                (from u in db.PMProcedures group u by new { PMId = u.PMId } into g select new { g.Key.PMId, MaintenanceCycleUnit = g.Max(p => p.MaintenanceCycleUnit), monthofDay = g.Max(p => p.monthofDay) })
11                            on a.PMId equals b.PMId
12                            select new { MOrder = a.MOrder, PMId = b.PMId, MaintenanceCycleUnit = b.MaintenanceCycleUnit, monthofDay = b.monthofDay })
13                             on t.MOrder equals c.MOrder
14                       join d in             //取得这张单的按周期或者按频率的数据
15                           (from u in db.PMServicings group u by new { MOrder = u.MOrder } into g select new { g.Key.MOrder, Result = g.Min(p => (p.Result == null ? "" : p.Result)), CMReason = g.Max(p => p.CMReason), CMDetailReason = g.Max(p => p.CMDetailReason), CMECDate = g.Max(p => p.CMECDate) })
16                       on t.MOrder equals d.MOrder
17                       into tb1
18                       from d in tb1.DefaultIfEmpty()
19                       select new { t, b, c, d }
20                       ;
21 
22             PropertyInfo[] infos = qq.ToList().First().GetType().GetProperties();
23             PropertyInfo[] modelinfos = typeof(MaintenanceRecord).GetProperties();
24             List<MaintenanceRecord> list = new List<MaintenanceRecord>();
25             foreach (var obj in qq)
26             {
27                 MaintenanceRecord model = new MaintenanceRecord();
28                 foreach (var item in modelinfos)
29                 {
30                     foreach (var infoitem in infos)
31                     {
32                         var infoObj = infoitem.GetValue(obj);
33                         PropertyInfo pitem = infoObj.GetType().GetProperties().Where(o => o.Name == item.Name).FirstOrDefault();
34                         if (pitem != null)
35                         {
36                             item.SetValue(model, pitem.GetValue(infoObj));
37                             break;
38                         }
39                     }
40                 }
41                 list.Add(model);
42             }
43             return list;
44         }
View Code
原文地址:https://www.cnblogs.com/Depingblogs/p/13274762.html