Project Summary-the legendary reflection is like this, and the project summary legend reflection

Source: Internet
Author: User

Project Summary-the legendary reflection is like this, and the project summary legend reflection

After the end of this sub-project, we began to summarize the various knowledge used in the project, such as AOP, CAS, SSO, multithreading, distributed cache, reflection, WCF, framework, database creation and sharding. Although some people do not know much about it, they generally have an understanding of it. Today, let's talk about the legendary reflection.
1. What is it (1) reflection mechanism in. NET
Reflection (Reflection) is. NET, which can be obtained at runtime through reflection. NET, including methods, attributes, events, and constructor. You can also obtain the name, qualifier, and parameters of each member. With reflection, you can be familiar with every type. If you obtain information about the constructor, you can directly create an object, even if the object type is unknown during compilation.
(2) reflection mechanism in Java
The JAVA reflection mechanism is in the running state. For any class, all attributes and methods of this class can be known. For any object, can call any of its methods and attributes. This kind of information obtained dynamically and the function of dynamically calling methods of objects is called the reflection mechanism of java language. JAVA reflection (radiation) mechanism: "When a program is running, the program structure or variable type can be changed. This language is called a dynamic language ". From this point of view, Perl, Python, and Ruby are dynamic languages, and C ++, Java, and C # Are not dynamic languages. However, JAVA has a very prominent Dynamic correlation mechanism: Reflection. in Java, it refers to classes that we can load, explore, and use completely unknown during runtime compilation. In other words, a Java program can load a class whose name is known only at runtime, and learn its complete structure (but does not include the definition of methods ), and generate its object entity, set its fields value, or arouse its methods.
(3) What is it?
Reflection refers to a type of applications that can be self-described and self-controlled. That is to say, this type of application uses a mechanism to describe and monitor its own behavior, and according to its own behavior status and results, adjusts or modifies the status and semantics of the behavior described by the application. It mainly refers to the ability of a program to access, detect, and modify its own state or behavior. For example, every one of us has taken a mirror, and the mirror shows our perfect body. So what is this achieved? All of us who have studied natural science in elementary school know it, this is caused by the reflection of light. This is reflection. Through reflection, we can clearly see the appearance of any object (attributes and methods ). Of course, the image in the mirror is not prepared to be put in advance, but is parsed and then displayed according to the existing object under certain conditions. This is reflection.
2. Why?
Why do we use this reflection. Previously, we learned the object-oriented idea and used encapsulation to separate the interior and exterior of an object, which reduces the coupling with the exterior and affects flexibility. For example, we need to modify the attributes or methods of a class or add a method to it, so we need to modify it in the code. This is especially difficult when we need to change it during the running process. As a result, reflection is coming. Just like in the real world, we need to package some things for people who do not need to know their internal structures. This is called encapsulation. Sometimes it is necessary to present a thing internally, understand its structure, and then make corresponding changes. This is called reflection.
3. Advantages and Disadvantages
Static Compilation: determine the type during compilation, bind the object, that is, pass. Dynamic Compilation: determines the type during runtime and binds the object. Dynamic compilation maximizes the flexibility of java, reflects the application of polymorphism, to reduce the coupling between classes. (1) Advantages:In a word, the advantage of the reflection mechanism is that it can achieve dynamic object creation and compilation, reflecting great flexibility. Especially in the Development of J2EE, its flexibility is very obvious. For example, it is impossible for a large software to design it perfectly once. When the program is compiled and released, when it finds that some functions need to be updated, we cannot ask the user to unmount the previous version and reinstall the new version. If so, this software is definitely not used by many people. If static is used, you need to recompile the entire program once to update the function. If reflection is used, it does not need to be uninstalled, this function can be implemented only when it is dynamically created and compiled at runtime. (2) Disadvantages:Its disadvantage is that it has an impact on performance. Reflection is basically an interpreted operation. We can tell JVM what we want to do and what it meets our requirements. This type of operation is always slower than simply performing the same operation.
4. Summary
Reflection mechanism is the principle and core part of the framework technology. Through the reflection mechanism, we can dynamically load classes, call class methods, and use class attributes by changing the configuration file (XML files later. In this way, it brings great convenience to coding and maintenance. When the program changes, it only changes the corresponding functions. The called method does not need to be changed. It will not change the whole body as soon as it is changed. Reflection is actually to parse the structure of a class or dll file through decompilation, and then obtain the corresponding elements through traversal. It obtains the required attributes and methods through dynamic parsing. Here, I would like to say that to learn a thing, we must combine the actual situation and contact our daily life to see how it came from and why it came about. Only in this way can I learn more and more easily.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.