c USES a reflection invocation type member example

  • 2020-06-07 05:14:18
  • OfStack

In real life, there are fewer opportunities to use reflection directly. I remember using reflection to dynamically get each control on the form and add registration events for the necessary controls once I made the WinForms gadget myself. Since I just joined the new company, in order to understand the company's business and development habits more quickly, I started to modify the small Bug of the existing system from my current colleague. One of the Bug submissions from Tester is a dynamic sort of GridView - a sort by clicking on a column using that column as a condition (PS: clicking on a column sends the column's string (the string is) to a method in the background).

Reasons for using reflection

Why do you choose reflection? In the project, we used NHibernate as the ORM framework. Generally, the corresponding sorting method is provided in the BLL layer. However, the data used by the modified Bug is not in one table, but in one view. If the pattern is implemented according to the previous sorting function, it needs to be modified in several places. And because I am new to this project, I do not know enough about the structure of the project, so it is not suitable to make extensive modifications. After talking to colleagues, I decided to use reflection. The reasons are as follows:

Because you are sorting the results that have been returned from the database directly, all you need to do is change them in one place (BLL layer)
Although there is a performance overhead associated with using reflection, the amount of data in this scenario is small and the performance cost is negligible

Use reflection to invoke code dynamically

Reflection may seem very complicated, but it is actually quite convenient to use. Due to too much code context involved in the project, it is not suitable for Posting. The following are some commonly used code fragments using reflection to make dynamic calls:


classProgram
{
staticvoidMain(string[]args)
{
Typetype=typeof(Employee);
// Create an object dynamically using a parameterless constructor 
varobjNull=type.InvokeMember(null,BindingFlags.CreateInstance,null,null,null);
// Calling two USES two string Parameter's constructor creates the object dynamically 
varfrankJob=type.InvokeMember(null,BindingFlags.CreateInstance,null,null,newobject[]{"job","frank"});
// Call the public member property get methods 
varfileName=type.InvokeMember("FirstName",BindingFlags.GetProperty,null,frankJob,null);
// Call the public member property set methods 
type.InvokeMember("Email",BindingFlags.SetProperty,null,frankJob,newobject[]{"gyzdfasddfsafhao@vervidian.com"});
// Invoke parameterless methods dynamically 
varobjStr=type.InvokeMember("ToString",BindingFlags.InvokeMethod|BindingFlags.Public|BindingFlags.Instance|BindingFlags.Static,null,frankJob,null);
// Dynamically invoke methods with parameters 
varemail=type.InvokeMember("GetEmail",BindingFlags.InvokeMethod,null,frankJob,newobject[]{"sunshine"});
}
publicclassEmployee
{
publicintId{get;set;}
publicstringFirstName{get;set;}
publicstringLastName{get;set;}
publicstringAddress{get;set;}
publicstringEmail{get;set;}
publicEmployee(){}
publicEmployee(stringfirstName,stringlastName)
{
FirstName=firstName;
LastName=lastName;
}
publicoverridestringToString()
{
returnstring.Format("{0}{1}",LastName,FirstName);
}
publicstringGetEmail(stringuser)
{
returnstring.Format("{0}@gmail.com",user);
}
}
}


Related articles: