C# 反射(Reflection)的用处分析
乱侃
作为一名新手,一直没有勇气去写一篇分享。原因有很多:诸如:自己水平有限、语言表达不准确、写出的东西没有一点技术点被人嘲笑。今天在公司听了内部员工的一个分享,其中最重要的一点是:提升自身水平的最佳的途径就是——交流。不管你是通过什么途径,交流也好、整理成文字分享也好等等都是很好的方式。故此,今天献丑写一篇自己的心得分享,欢迎各路大神的指教!
需求背景
今天接到的需求里面有个这样的需求,如下图所示,需要打印出如excel内容呈现的单据。
动手操作第一版本
而为了实现这个业务需要涉及三张表的数据。(存放单据的表、审核意见表、审核状态表)
三张表的关系:单据表1:1审核状态表,单据表1:n审核意见表
为了实现让view页面整洁,我定义了一个specialprintmodel类
public class specialprintmodel { /// <summary> /// 供应商承担 /// </summary> public string supplierundertaker { get; set; } /// <summary> /// 客户订单号 /// </summary> public string customerserialnumber { get; set; } /// <summary> /// 付款金额 /// </summary> public decimal payamount { get; set; } /// <summary> /// 开户行 /// </summary> public string openingbank { get; set; } /// <summary> /// 收款单位 /// </summary> public string collectionmonad { get; set; } /// <summary> /// 银行帐号 /// </summary> public string bankaccount { get; set; } /// <summary> /// 经办人 /// </summary> public string responseibleperson { get; set; } /// <summary> /// 分管领导 /// </summary> public string leader { get; set; } /// <summary> /// 财务审核 /// </summary> public string financeapproval { get; set; } /// <summary> /// 财务经理审核 /// </summary> public string financemanagerapproval { get; set; } /// <summary> /// 财务总监审核 /// </summary> public string financedirectorapproval { get; set; } /// <summary> /// ceo审核 /// </summary> public string ceoapproval { get; set; } /// <summary> /// 流水号 /// </summary> public string serialnumber { get; set; } }
public list<shipspecialprintmodel> gettobepaidrecepit(shipspecialsearch search) { list<shipspecialprintmodel> curiseshipmodel = new list<shipspecialprintmodel>(); var tobepaidmodel = persistant.gettobepaidrecepit(search);//查找出待支付的单据表信息 arraylist serialarray=new arraylist();//定义一个流水号列表 tobepaidmodel.foreach((u) => { serialarr.add(u.serialnumber); }); var tobepaidcomment = persistant.gettobepaidrecepitcomment(serialarr);//查找出待支付单据的审核意见表(1个单据对应多少审核意见) foreach (var item in tobepaidmodel) { shipspecialprintmodel temp = new shipspecialprintmodel() { supplierundertaker = supplierundertaker; customerserialnumber = item.customerordernumber; payamount = item.payamount; openingbank = item.paybank; collectionmonad = item.payee; responseibleperson = item.creator; serialnumber = item.serialnumber; }; curiseshipmodel.add(temp); } foreach (var curise in curiseshipmodel) { foreach (var comment in tobepaidcomment) { if (comment.serialnumber == curise.serialnumber) { if (comment.approvallevel == (int)leveltype.branchedleader) { curise.leader = comment.creator; } else if (comment.approvallevel == (int)leveltype.finance) { curise.financeapproval = comment.creator; } else if (comment.approvallevel == (int)leveltype.financemanager) { curise.financemanagerapproval = comment.creator; } else if (comment.approvallevel == (int)leveltype.projectdirector) { curise.financedirectorapproval = comment.creator; } else if (comment.approvallevel == (int)leveltype.ceo) { curise.ceoapproval = comment.creator; } } } } return curiseshipmodel }
呵呵,上面的代码基本完成了业务的需求,可是如果业务需要打印出cto的名称、cio的名称那在if else这边加,虽然很简单但是违背了开放-封闭的原则。故本人决定用反射去完成这if...else的事情。
因为if...else里面的判断是当前的这笔单据的审核意见表的层级是不是跟specialprintmodel的字段所对应的层级相等,若相等则在对应字段写入相对应的名称。决定把specialprintmodel这个类修改下。
动手操作第二版本
public class shipspecialprintmodel { /// <summary> /// 供应商承担 /// </summary> public string supplierundertaker { get; set; } /// <summary> /// 客户订单号 /// </summary> public string customerserialnumber { get; set; } /// <summary> /// 付款金额 /// </summary> public decimal payamount { get; set; } /// <summary> /// 开户行 /// </summary> public string openingbank { get; set; } /// <summary> /// 收款单位 /// </summary> public string collectionmonad { get; set; } /// <summary> /// 银行帐号 /// </summary> public string bankaccount { get; set; } /// <summary> /// 经办人 /// </summary> public string responseibleperson { get; set; } /// <summary> /// 分管领导 /// </summary> [levelattribute(level = 1)] public string leader { get; set; } /// <summary> /// 财务审核 /// </summary> [levelattribute(level = 2)] public string financeapproval { get; set; } /// <summary> /// 财务经理审核 /// </summary> [levelattribute(level = 3)] public string financemanagerapproval { get; set; } /// <summary> /// 财务总监审核 /// </summary> [levelattribute(level = 4)] public string financedirectorapproval { get; set; } /// <summary> /// ceo审核 /// </summary> [levelattribute(level = 5)] public string ceoapproval { get; set; } /// <summary> /// 流水号 /// </summary> public string serialnumber { get; set; } } public class levelattribute : attribute { public int level { get; set; } }
var tobepaidcomment = persistant.gettobepaidrecepitcomment(arraylist.adapter(tobepaidmodel.select(u => u.serialnumber).tolist())); var specialpropertyinfo = (from property in typeof(curiseshipspecialprintmodel).getproperties() where property.getcustomattributes(typeof(levelattribute), false).count() > 0 select property).tolist(); tobepaidmodel.foreach((item)=>{ shipspecialprintmodel temp = new shipspecialprintmodel() { supplierundertaker = supplierundertaker; customerserialnumber = item.customerordernumber; payamount = item.payamount; openingbank = item.paybank; collectionmonad = item.payee; responseibleperson = item.creator; serialnumber = item.serialnumber; }; var thiscomments=tobepaidcomment.where(u=>u.serialnumber =item.serialnumber ).tolist(); thiscomment.foreach((cm)=> { if(cm.approvallevel==(specialpropertyinfo.getcustomattributes(typeof(levelattribute),false).first() as levelattribute).level) { cm.setvalue(model,cm.creator,null); } }); })
然而看到,propertyinfos那边基本上每循环一次都需要去反射查找下元素,为了避免这样的性能消耗,决定再修改一翻,定义一个字典去存储specialprintmodel标有特性类的字段。
动手操作第三版本
dictionary<int, propertyinfo> dic = new dictionary<int, propertyinfo>(); propertyinfos.foreach((myproperty) => { dic.add((a.getcustomattributes(typeof(levelattribute),false).first() as levelattribute).level,myproperty)); } ); comments.foreach((cm) => { if (dic.keys.contains(cm.approvallevel)) { dic[cm.approvallevel].setvalue(model, cm.creator, null); } });
总体经过三次的修改,已经避免if...else的代码。这样而言,也比较适合后面比如再需要打印cto审核的名称。那样只需要再model类里面填写字段与在字段上加上个特效。
总结
就像我同事说的那样,做任务事情只要想想、多敲几次。一些问题就不是问题了。好了,今天就写到这里吧。晚安各位!
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!