Reflection is a very interesting thing. Most of the interesting things can be associated with each other.
In the Journey to the West, Monkey Sun will change in. Reflection may also work.
Under the pen of God, all things are revived. Oh, reflection is sometimes the pen.
What surprised me most when I first came into contact with Java was its reflection.
For the first time in contact with. net, see reflection.
It seems that you should think about it carefully after you get back to life and get familiar with it.
What is reflection for half a day?
Close your eyes and think about it ......
Reflection, running EnumerationProgramStructure, which is generated during execution without CompilationCode.
I have racked my brains and thought of the descriptive method.
Don't you understand? Ele. Me!
The ugly old witch began to read the spell:
I want to become a pretty girl. I'm going to join super girl voice ......
She didn't go to the beauty salon or participate in the Supergirl. Under the reflection of a magic mirror, a beautiful girl appeared in front of me.
Okay, serious. Unorganized, reflection-based functions. For me, some interesting and boring applications are possible.
I. Enumeration program structure.
Well, some code-level stuff is found during running.
. Net, from the current application domain, through reflection, you can enumerate all the assembly (distribution package), and then, in the Assembly, you can enumerate all modules (DLL or EXE level), all types (class), types, and all members (attributes, methods, etc.) in the module ......), Or an interface or nested class, all the parameters in the method (constructor, attribute, event.
It should be easy. In the end, the general framework of the. Net program can be obtained through reflection.
Let's talk about its usefulness.
1.1: decompile the Il Language
Yes, of course you have used ildasm, the. NET il language decompilation tool.
In fact, after loading the program (.. Net), through reflection, combined with public managed modules and Assembly formats for method il code analysis, you can complete the customization of your own decompilation tool. Of course, whether it makes sense is perhaps just a dazzling one, or a better understanding of this aspect.
1.2: The Designer (or other scenarios) dynamically provides type features
In a designer similar to visal studio, you can enumerate the attributes, methods, and events of the UI unit of the Form Designer through anti-reflection ......, Of course, you can implement it by expanding the data structure, but if reflection is used, your code will be simpler, no data redundancy, better scalability, and clearer design ideas, some people say, code is the best design document. The code is directly reflected into the application, which is obviously the most efficient in design.
1.3: Provides the sensing code.
In VB and some advanced development languages, the typelib of COM is used to implement sensing encoding (the methods, attributes, and events of the COM interface are enumerated dynamically ). Now, with reflection and metadata of. net, the typelib method of COM is too redundant and complicated. Reflection can be easily done.
Ii. Dynamic Code Execution.
Reflection, You can dynamically generate type instances based on the strings provided during the runtime, and dynamically execute the specified method.
It is short and meaningful. Let's talk about its possible derivation.
2.1: Late program binding
In Win32, we used loadlibrary and getprocaddress to call method late binding.
Reflection is similar to late binding. The advantage of late binding is that loading the Assembly, Constructing Type instances, and calling methods may be used in analyzing program structures.
2.2: design mode and reflection
Furthermore, in the design model, we like to use factory methods to build instances and abstract factory subclasses. You can run commands in command mode and generate different entities in builder mode. In fact, all of these can be simplified Using Reflection (instead of writing a specific method ).
Of course, before selecting this option, you may lose some performance.
2.3: serialization.
There is serialization in MFC, but it does not provide any guidance, and it is very inconvenient to expand. With the help of reflection, Field Enumeration During serialization becomes simple, and object generation during deserialization is also easy. Of course, related applications have been stored in FCL. In many cases, you may not feel the benefits of reflection.
2.4: Communication in heterogeneous systems
The three-tier architecture supported by DCOM has brought about Windows's previous boom. However, related deployment upgrades, including the complexity of their internal implementation principles, are always scratching your head. In addition, under the pressure of Java J2EE,. net has been improved.
. NET provides a new remote communication method (remoting) and powerful Web Support (Web XML ). It is said that reflection is widely applied to all of them. In this case, I don't have time to check it carefully. It is estimated that reflection is used in the column set and the inverse column set. Of course, there must be a lot of new things, so I have no time to see them.
However, I used to have access to a simple application of reflection in heterogeneous systems.
The client uses HTML in the web browser to send a command (custom XML format) program with the simplest http post. XML parsing on the server end actually specifies the assembly, A call of the specified type. In this way, the client does not need to install any type. You only need to generate code in the specified format. A system that passes through the firewall is implemented. The key is reflection.
2.5: special applications in tezhu
Reflection has some special functions in some special cases. For example, there is an example in this book. The static class is used to bind the delegate, and the code needs to actively trigger the static constructor of the static class, it can be achieved through reflection (createinstance.
This is probably the case. If you see the new stuff, add it.