1. Background
When learning C #, we know that using reflection can greatly facilitate our programming (dynamic access to information, call type members, instance creation, etc ), however, there are few opportunities to directly use reflection in actual work. The impression is that reflection is used to dynamically obtain each control on the form when you make your own winforms gadgets, and dynamically add registration events for necessary controls. To get started with a new company and learn more about the company's business and development habits, we should first work with our colleagues to modify some minor bugs in the existing system. One of the bugs submitted by tester is to dynamically sort the gridview. When a column is clicked, this column is used as the condition for sorting (PS: when you click a column, the foreground will upload the string of the column (the string is) to the background method ).
2. Reasons for Using Reflection
Why does it use reflection? In the project, we use nhib.pdf as the orm framework. Generally, the BLL layer provides the corresponding sorting method, however, the data used by the bug to be modified is not in a table, but a view. If you follow the previous sorting implementation mode, you need to modify it in multiple places. Because you are new to this project, you are not familiar with the structure of the project and should not modify it in a large area. After communicating with colleaguesUse reflection. The reasons are as follows:
- Because it is directly sorting the results returned from the database, all needs to be modified in one place (BLL layer)
- Although reflection increases performance overhead, the data volume in this scenario is not large, and the performance consumption is negligible.
3. Dynamic call Using Reflection Code
It seems complicated to reflect to people. In fact, it is quite convenient to use. Because the project involves too many code contexts, it is not suitable for posting, below are some common code snippets that I have compiled and used reflection for dynamic calling:
1 Class Program 2 { 3 Static Void Main ( String [] ARGs) 4 { 5 Type type = Typeof (Employee ); 6 7 // Dynamically create an object using a non-argument Constructor 8 VaR Objnull = type. invokemember ( Null , Bindingflags. createinstance, Null , Null , Null ); 9 10 // Call two constructors that use two string parameters to dynamically create an object 11 VaR Frankjob = type. invokemember ( Null , Bindingflags. createinstance, Null , Null , New Object [] { " Job " , " Frank " }); 12 13 // Call the get Method for Public Member attributes 14 VaR Filename = type. invokemember ( " Firstname " , Bindingflags. getproperty, Null , Frankjob, Null ); 15 16 // Call the Set Method of Public Member attributes 17 Type. invokemember ( " Email " , Bindingflags. setproperty, Null , Frankjob, New Object [] { " Gyzhao@vervidian.com " }); 18 19 // No-Parameter Method for Dynamic call 20 VaR Objstr = type. invokemember (" Tostring " , Bindingflags. invokemethod | bindingflags. Public | bindingflags. instance | bindingflags. Static, Null , Frankjob, Null ); 21 22 // Dynamic call of methods with Parameters 23 VaR Email = type. invokemember ( " Getemail " , Bindingflags. invokemethod, Null , Frankjob, New Object [] { " Sunshine " }); 24 } 25 26 Public Class Employee 27 { 28 Public Int Id { Get ; Set ;} 29 Public String Firstname { Get ; Set ;} 30 Public String Lastname { Get ; Set ;} 31 Public String Address { Get ; Set ;} 32 Public String Email { Get ; Set ;} 33 34 Public Employee (){} 35 Public Employee ( String Firstname, String Lastname) 36 { 37 Firstname = Firstname; 38 Lastname = Lastname; 39 } 40 41 Public Override String Tostring () 42 { 43 Return String . Format ( " {0} {1} " , Lastname, firstname ); 44 } 45 46 Public String Getemail ( String User) 47 { 48 Return String . Format ( " {0} @ gmail.com " , User ); 49 } 50 } 51 }
References & further reading
Reflection in. NET Framework