Usage of C # Reflection

Source: Internet
Author: User

Usage of C # Reflection
The first version requires three tables to implement this service. (The table that stores the document, the audit suggestion table, and the audit status table) relationship: the document table audit status table, the Document Table 1: N audit suggestion table in order to make the View page clean, I have defined a SpecialPrintModel class to copy the code public class SpecialPrintModel {// <summary> // vendor undertakes /// </summary> public string SupplierUnderTaker {get; set ;} /// <summary> /// customer order No. /// </summary> public string CustomerSerialNumber {get; set ;} /// <summary> /// payment amount /// </summary> public decimal PayAmount {get; set ;}/// <summary> /// account opening bank /// </Summary> public string OpeningBank {get; set ;}/// <summary> /// receiving unit /// </summary> public string CollectionMonad {get; set ;}//< summary> /// bank account /// </summary> public string BankAccount {get; set ;} /// <summary> /// operator /// </summary> public string ResponseiblePerson {get; set ;} /// <summary> /// Leader in charge /// </summary> public string Leader {get; set ;} /// <summary> // financial audit /// </Summary> public string FinanceApproval {get; set ;}/// <summary> // financial manager review /// </summary> public string FinanceManagerApproval {get; set ;}/// <summary >/// Financial Director review /// </summary> public string FinanceDirectorApproval {get; set ;} /// <summary> // CEO review // </summary> public string CEOApproval {get; set ;} /// <summary> /// serial number /// </summary> public string SerialNumber {get; set ;}} copy the code Copy the public List <ShipSpecialPrintModel> gettobepaidresponse it (ShipSpecialSearch search) {List <ShipSpecialPrintModel> curiseShipModel = new List <ShipSpecialPrintModel> (); var toBePaidModel = persistant. gettobepaidduplicate it (search); // you can find the information of the ticket table to be paid. ArrayList serialArray = new ArrayList (); // you can define a list of sequential numbers. forEach (u) => {serialArr. add (u. serialNumber) ;}); var toBePaidComment = persistant. getTobePa IdRecepitComment (serialArr); // find the form of comments to be paid (the number of comments for each document) 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}: copy the code. The above code basically meets the business requirements, however, if the business needs to print the CTO name and CIO name, add it on the if else side, although it is very simple. But it violates the principle of openness and closures. Therefore, I decided to use reflection to complete this if... else task. Because if... the else determines whether the level of the audit suggestion table of the current document is equal to that of the SpecialPrintModel field. If the level is equal, the corresponding name is written to the corresponding field. Decided to modify the SpecialPrintModel class. Step 2: copy the public class ShipSpecialPrintModel {// <summary> // The supplier undertakes the code. /// </summary> public string SupplierUnderTaker {get; set ;} /// <summary> /// customer order No. /// </summary> public string CustomerSerialNumber {get; set ;} /// <summary> /// payment amount /// </summary> public decimal PayAmount {get; set ;} /// <summary> /// opening bank /// </summary> public string OpeningBank {get; set ;}/// <summary> /// receiving ticket Bit /// </summary> public string CollectionMonad {get; set ;} /// <summary> /// bank account /// </summary> public string BankAccount {get; set ;} /// <summary> /// operator /// </summary> public string ResponseiblePerson {get; set ;} /// <summary> // in charge of the leadership // </summary> [LevelAttribute (Level = 1)] public string Leader {get; set ;} /// <summary> // financial audit // </summary> [LevelAttribute (Level = 2)] public str Ing FinanceApproval {get; set ;}/// <summary> // audit by the Financial Manager /// </summary> [LevelAttribute (Level = 3)] public string FinanceManagerApproval {get; set ;}/// <summary> // Financial Director review /// </summary> [LevelAttribute (Level = 4)] public string FinanceDirectorApproval {get; set ;}/// <summary> // CEO review /// </summary> [LevelAttribute (Level = 5)] public string CEOApproval {get; set ;}/// <summary> /// streamline No. /// </summary> public string SerialNumber {get; set ;}} public class LevelAttribute: Attribute {public int Level {get; set ;}} copy code copy code var toBePaidComment = persistant. gettobepaidincluitcomment (ArrayList. adapter (toBePaidModel. select (u => u. serialNumber ). toList (); var specialPropertyInfo = (from property in typeof (CuriseShipSpecialPrintModel ). getProperties () where property. getCustomAttri Butes (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. se RialNumber ;}; 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) ;}});}) copy the Code. However, we can see that propertyInfos basically needs to reflect and find the elements at each loop. To avoid such performance consumption, I decided to modify it again and define a dictionary to store the SpecialPrintModel marked with a feature class.. Copy the code Dictionary <int, PropertyInfo> dic = new Dictionary <int, PropertyInfo> (); propertyInfos in version 3. forEach (myProperty) => {dic. add (. 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) ;}}); the overall copy code has been modified three times to avoid if... else. In this case, the CTO review name should be printed later, for example. In this way, you only need to fill in the field in the Model class and add a special effect on the field.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.