"C #": On the reflection mechanism "turn"

Source: Internet
Author: User

http://blog.csdn.net/lianjiangwei/article/details/47207875

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 the type to an existing object, or get the 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.

The reflection mechanism is. NET to get run-time type information. NET application consists of several parts: ' Assembly (Assembly) ', ' module ', ' type ', and reflection provides a programmatic way for programmers to obtain information about these components during the program's run time.

In layman's terms, reflection is an object that understands the internal structure and information of another object, even if you do not know where that object exists. Like a ship's sonar probe, we don't know what's going on in the depths of the ocean, but by reflecting back through the sonar, we can grasp the size of the obstacle, the speed of movement, and so on, and the reflection we're talking about is a truth. But we can use reflection in the program not only to get the internal condition of the class, but also to manipulate the method of invoking the reflection class, which is simply amazing.

Why use reflection?

1 After using the reflection, your program in the update without recompiling, as long as the updated DLL replaced to complete the program update and upgrade.

2 By combining the reflection and configuration files, we can develop the software with the function of adding and deleting functions, which has great flexibility and expansibility.

The 3 reflection improves the flexibility of the program, reduces the coupling, improves the adaptive ability, and greatly improves the reusability of the program.

But one thing to emphasize is that everything has two sides, not all of which are suitable for use in reflective technology. Using reflection also has its drawbacks:

1 One of the big problems that comes first is the performance issue, which is basically an interpretation operation for fields and methods that are much slower to access than direct code.

2 using reflection causes the internal logic of the program to be blurred, and the programmer Sivayan to see clear program logic when debugging the code, while reflection bypasses the source code, which leads to some maintainability problems, while the reflection code is more complex than the corresponding direct code.

In summary: The reflection mechanism has its application, as long as it is used to develop software systems with high flexibility and scalability requirements, ordinary programs do not need to use.

How to use reflection?

First of all, I am not very deep in the use of reflection, just a few simple attempts, the following with a small demo to illustrate how to use reflection in C # to get a class of properties, methods and modify properties and call methods and so on. The code is as follows:

First, we'll write a target class student

[CSharp]View PlainCopy
  1. <span style="FONT-SIZE:18PX;"  >using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Namespace Itoo. Reflection.student
  6. {
  7. public class Student
  8. {
  9. public string Name { get;  set;}
  10. public int Age { get;  set;}
  11. //default constructor
  12. Public Student ()
  13. {
  14. This .  Age = 24;
  15. This .  Name = "Even Giang Wei";
  16. }
  17. //constructors with parameters
  18. Public Student (string name,int. age)
  19. {
  20. This .  name = name;
  21. This .  Age = age;
  22. }
  23. public void Hello ()
  24. {
  25. Console.WriteLine ("I am" +name + ", I am this year" +age +"years old!  ");
  26. }
  27. }
  28. }</span>

Then we use reflection to manipulate the class in the console application.

[CSharp]View PlainCopy
  1. <span style="FONT-SIZE:18PX;"  >using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Reflection;
  6. Namespace Itoo. Reflection.client
  7. {
  8. Class Program
  9. {
  10. static void Main (string[] args)
  11. {
  12. //Dynamic load DLL, this loadfile is the most convenient, parameter is the path of the DLL.
  13. var asm = assembly.loadfile (@"C:\Users\ljw\Desktop\ Learning example \itoo. Reflection.test\itoo. Reflection.student\bin\debug\itoo.  Reflection.Student.dll ");
  14. //Get the type of the student class
  15. var type = asm. GetType ("Itoo.  Reflection.Student.Student ");
  16. //Create an instance of the class
  17. var instance = asm. CreateInstance ("Itoo.  Reflection.Student.Student ");
  18. //Assign values to student class properties
  19. Type. GetProperty ("Name").  SetValue (instance, "Duan Tianya", null);
  20. Type. GetProperty ("age").  SetValue (instance, +, null);
  21. //Get the method of student class
  22. var method = type.  GetMethod ("Hello");
  23. //Call the member method of the student class Hello
  24. Method.  Invoke (instance, null);
  25. Console.read ();
  26. }
  27. }
  28. }</span>

Summary: for the concept of reflection, I was relatively vague at first, although many times to hear others in the talk, but because no one to check the data to study, to do the code to achieve, after all, can not empathize, understand the principles and ideas, now read a large number of blogs and articles, and then hands-on practice a bit, With some shallow understanding, due to the donet reflection mechanism is encapsulated well, so the reflection of the internal implementation of the principle is not clear, look forward to continue to further study.

"C #": On the reflection mechanism "turn"

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.