what is reflection.
Reflection provides objects (type types) that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind a type to an existing object, or get a type from an existing object and call its methods or access its fields and properties. If you use attributes in your code, you can use reflection to access them.
Reflex mechanism is. NET to get Run-time type information. NET application consists of several parts: ' Assembly (Assembly) ', ' module ', ' type ' (Class), and reflection provides a programmatic way for programmers to obtain relevant information about these components during the program run time.
In layman's terms, reflection is an object that understands the internal structure and information of another object, even when you do not know the object exists. Like a ship's sonar, we do not know what is deep in the sea, but by the reflection back of the sonar, we can grasp the size of the obstacles, the speed of movement and so on, and we are talking about the reflection is a truth. But we can not only use reflection in the program to get, the internal situation of the class, but also to manipulate the call to reflect the class method, it is amazing.
why use Reflection.
1 after using reflection, your program does not need to be recompiled at the time of the update, as long as the updated DLL is replaced to complete the program update and upgrade.
2 The combination of reflection and configuration files, can develop the function of random additions and deletions of software, with a great flexibility and scalability.
3 reflection improves the flexibility of the program, reduces the coupling, improves the self-adaptive ability, and improves the reusability of the program greatly.
But the point to emphasize is that everything has two sides, not all occasions are suitable for using reflective technology. Using reflection can also have its drawbacks:
A big problem with 1 is the performance problem, where reflection is basically an interpretation operation that is much slower than direct code for field and method access.
2 using reflection will make the internal logic of the program fuzzy, the programmer in debugging the code when Sivayan see clear program logic, and reflection bypassing the source code, so there will be some maintenance problems, while the reflection code more complex than the corresponding direct code.
To sum up: reflection mechanism has its application, as long as the development of flexibility and scalability of high requirements of software systems, ordinary programs are not necessary to use.
How to use reflection.
First of all, I am not very deep in the use of reflection, but do some simple attempts, below a small demo to illustrate how to use reflection in C # to get a class of properties, methods, and modify properties and call methods. The code is as follows:
First we write a target class student
<span style= "FONT-SIZE:18PX;" >using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace Itoo. Reflection.student
{public
class Student
{public
string Name {get;
public int Age {get; set;}
The default constructor public
Student ()
{this
. Age =;
This. Name = "Even Giang Wei";
}
constructor with parameters public
Student (String Name,int age) {This
. name = name;
This. Age = Age;
}
public void Hello ()
{
Console.WriteLine ("I am +name +"), I am "+age +" year old. ");
}
}
} </span>
And then we use reflection to manipulate this class in the console application.
<span style= "FONT-SIZE:18PX;"
>using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Reflection; Namespace Itoo. Reflection.client {class Program {static void Main (string[] args) {//DLL dynamically loaded, this load
File is most convenient, and the parameter is the path to the DLL. var asm = assembly.loadfile (@ "C:\Users\ljw\Desktop\ Learning example \itoo.") Reflection.test\itoo. Reflection.student\bin\debug\itoo.
Reflection.Student.dll "); Gets the type var type = ASM for the Student class. GetType ("Itoo.")
Reflection.Student.Student "); Create an instance of this class var instance = asm. CreateInstance ("Itoo.")
Reflection.Student.Student "); Assigns a value type to the Student class's property. GetProperty ("Name").
SetValue (instance, "Duan Tianya", null); Type. GetProperty ("Age").
SetValue (instance,%, null); Gets the methods var method = Type for the student class.
GetMethod ("Hello"); Invokes the member method of the student class, Hello methods.
Invoke (instance, null); Console.reAD (); }}}</span>
Summary: for reflection this concept, at first I was more vague, although many times to hear people talking in the talk, But because there is no oneself to check the data to study, to do the code to achieve, after all, can not feel empathy, understand the principles and ideas, now read a lot of blogs and articles, and then hands-on practice, with some shallow understanding, because the donet reflection mechanism is encapsulated good, So for the reflection of the internal implementation of the principle is not clear, look forward to continuing to further study.