Project summary--the legendary reflection is like this

Source: Internet
Author: User

at the end of the project, we began to summarize the various knowledge that was used in the project, such as Aop,cas,sso, multi-threading, distributed caching, reflection, WCF, Framework, Çeku, and so on. Although some knowledge is not very deep understanding, but generally have a understanding. So today, let's talk about the legendary reflection.
1, what is (1). The reflection mechanism in net
Reflection (Reflection) is an important mechanism in. NET that, through reflection, can get members of each type in. NET, including classes, structs, delegates, interfaces, and enumerations, including methods, properties, events, constructors, and so on, at run time. You can also get the names, qualifiers, parameters, and so on for each member. With reflection, you can get a sense of each type. If you get the information for the constructor, you can create the object directly, even if the type of the object is not known at compile time.
(2) reflection mechanism in Java
The
Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are called the reflection mechanisms of the Java language. Java Reflection (emission) mechanism: "When a program is run, it is allowed to change the program structure or variable type, which is called Dynamic language." From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language. But Java has a very prominent dynamic-related mechanism: Reflection, in Java, refers to the classes that we can load, detect, and use completely unknown during compilation at runtime. In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods.
(3) What exactly is
reflection refers to a class of applications that are self-describing and self-controlling. In other words, this kind of application uses some mechanism to realize the description (self-representation) and monitoring (examination) of its own behavior, and can adjust or modify the state and related semantics of the behavior described by the application according to the state and result of its behavior.         The main point is the ability of a program to access, detect, and modify its own state or behavior. For example, each of us has looked in the mirror, the mirror will show our perfect figure, then this is through what to achieve, the elementary school has learned the natural science know, this is the reflection of light caused.         This is the reflection. By reflection, we can clearly see the look of any object (properties and methods). Of course, the image in the mirror is not ready to put in advance, but according to the existing object, under certain conditions, the object of the resolution of all things, and then show out. This is the reflection.
2. Why
How does this reflection come about, and why should we use it? Before we learned about object-oriented thinking, we used encapsulation to separate the inside and outside of an object, thus reducing the coupling to the outside, and it also affected the flexibility, for example, if we needed to modify the properties or methods of a class or add a method to it, we would need to modify it in the code. Especially when we need to change it while we're running, it's even more difficult.         So the reflection came. Just like the real world, we need to wrap things up for people who don't need to know their internal structure, which is called encapsulation. And sometimes you need to show the inside of something, understand its structure, and make a corresponding change, which is called reflection.
3. Advantages and Disadvantages
Static compilation: Determines the type at compile time, binds the object, which is passed. Dynamic compilation: The runtime determines the type, binding object.         Dynamic compilation maximizes the flexibility of Java and embodies the application of polymorphism, which reduces the coupling between classes. (1) Advantages: in a word, the advantage of reflection mechanism is that it can achieve dynamic creation of objects and compilation, showing great flexibility, especially in the development of Java EE, its flexibility is very obvious. For example, a large-scale software, it is impossible to design the perfect one at a time, when the program compiled, released, when the discovery needs to update some features, we can not want users to uninstall the previous, and then reinstall the new version, if so, this software is certainly not many people use.         Using static words, the entire program needs to be recompiled once to achieve the function of the update, and the use of reflection mechanism, it can not uninstall, only need to be dynamically created and compiled at run time, you can implement this function. (2) Disadvantage: Its disadvantage is that it has an impact on performance. Using reflection is basically an interpretation operation, and we can tell the JVM what we want to do and it satisfies our requirements. This type of operation is always slower than just performing the same operation directly.
4. Summary
The reflection mechanism is the principle and core part of the framework technology. The reflection mechanism allows us to dynamically load classes, invoke class methods, and use class properties by changing the way configuration files (later, XML files) are changed. This provides considerable convenience for coding and maintenance. When the program changes, it will only change the corresponding function on the line, the method of invocation is not changed.         Not change the whole body. Reflection is actually the structure of a class or DLL file that is parsed out by an anti-compilation and then traversed to get the corresponding element.         It is through dynamic parsing to get the properties and methods we need. Here, I would like to say that learning a thing, must be combined with the actual, contact life, see how it comes, why to have. Only in this way can my study be more and more relaxed.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project summary--the legendary reflection is like this

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.