C #-Reflection (2)

Source: Internet
Author: User

1. What is Reflection

You can ildasm the Anti-compilation tool to explore the composition of a DLL and EXE.

Used to programmatically obtain type information at run time. is to load the out-of-program Assembly (DLL), and then invoke the public properties, methods, and so on in the assembly dynamically. Reflection describes the process of checking and handling program elements during a run, which is a run-Library discovery process.

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. You can also get the names, qualifiers, parameters, and so on for each member. With reflection, you can know every type at your fingertips. If you get the constructor information, you can create the object directly, even if the type of the object is not known at compile time.

is to obtain the type or instance information dynamically based on the metadata of the type//For example, public class myclass{public void SayHello () {}} Now I know it has a way of SayHello, then I can call it by reflection Method. In general: MyClass MyClass = new MyClass (); Myclass.sayhello (); MyClass myclass=new MyClass by Reflection (); Type type=typeof (MyClass); GetMethod ("SayHello"). Invoke (Myclass,null);

2, the program code after compiling to generate the executable application, we first want to understand the structure of this executable application.

Application architecture is divided into application domains-assemblies-modules-types-members of several levels, the common language runtime loader manages application domains, which include loading each assembly into the appropriate application domain and controlling the memory layout of the type hierarchy in each assembly.

Assemblies contain modules, and modules contain types, and types also contain members, and reflection provides objects that encapsulate assemblies, modules, and types. We can use reflection to dynamically create an instance of a type, bind the type to an existing object or get a type from an existing object, and then invoke a method of the type or access its fields and properties. Reflection typically has the following uses.

(1) Use assembly to define and load assemblies, load list modules in an assembly manifest, and find types from this assembly and create instances of that type.
(2) Use the module to understand the assemblies that contain modules, the classes in modules, and so on, and to get all global methods or other specific non-global methods defined on the module.
(3) Use ConstructorInfo to understand the name of the constructor, arguments, access modifiers (such as pulic or private), and implementation details (such as abstract or virtual). Use the GetConstructors or GetConstructor method of type to invoke a specific constructor.
(4) Use MethodInfo to understand the name of the method, the return type, parameters, access modifiers (such as pulic or private), and implementation details such as abstract or virtual. Use the GetMethods or GetMethod method of type to invoke a specific method.
(5) Use Fiedinfo to understand the name of a field, access modifiers (such as public or private) and implementation details (such as static), and get or set field values.
(6) Add or remove event handlers by using EventInfo to understand the name of the event, the event handler data type, the custom attribute, the claim type, and the reflection type.
(7) Use PropertyInfo to understand the name, data type, claim type, reflection type, and read-only or writable state of the property, and to get or set the property value.
(8) Use ParameterInfo to understand the name of the parameter, the data type, whether it is an input parameter or an output parameter, and the position of the parameter in the method signature.

Classes in the System.Reflection.Emit namespace provide a special form of reflection that can be constructed at run time.
Reflection can also be used to create an application called a type browser that enables the user to select a type and then view information about the selected type.
In addition, language compilers such as JScript use reflection to construct symbol tables. Classes in the System.Runtime.Serialization namespace use reflection to access the data and determine which fields to persist, and the classes in the System.Runtime.Remoting namespace use reflection indirectly through serialization.


Reference:
http://blog.csdn.net/helloguonan/article/details/5912523

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1655189

C #-Reflection (2)

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.