Reflection, java reflection

Source: Internet
Author: User

Reflection, java reflection
Reflection

The classes in the System. Reflection namespace and the System. Type namespace allow you to obtain information about the loaded assembly and the types defined in the namespace, such as classes, interfaces, and value types. You can use reflection to create, call, and access type instances at runtime.

CLR programs manage application domains. Application domains constitute the boundaries defined around objects with the same application scope. This management includes loading each assembly to the corresponding application domain and controlling the memory layout of the type hierarchy within each assembly.

 

The Assembly contains modules and modules, and the type contains members. Reflection provides encapsulated assembly, module, and type objects. You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from an existing object. Then, you can call methods of the type or access its fields and attributes. Typical reflection usage:

  • Use Assembly to define and load an Assembly, load the modules listed in the Assembly list, locate a type in the Assembly, and create an instance of the Assembly.

  • Use Module to discover information, such as modules and classes. You can also obtain all global methods or other specific non-Global methods defined on the module.

  • Use ConstructorInfo to discover information, such as names, parameters, and access modifiers (suchPublicOrPrivateAnd constructor implementation details (suchAbstractOrVirtual). Use the GetConstructors or GetConstructor method of Type to call a specific constructor.

  • Use MethodInfo to discover information, such as name, return type, parameter, and access modifier (for examplePublicOrPrivate) And method implementation details (suchAbstractOrVirtual). Use the GetMethods or GetMethod method of Type to call a specific method.

  • Use FieldInfo to discover information, such as names and access modifiers (suchPublicOrPrivate) And a field implementation details (suchStatic), And obtain or set the field value.

  • Use EventInfo to discover information (such as name, event handler data type, custom features, declared type, and event reflection type), and add or delete event handlers.

  • Use PropertyInfo to discover information (such as name, data type, declaration type, reflected type and attribute read-only or writable state), and obtain or set attribute values.

  • Use ParameterInfo to discover information, such as the parameter name, data type, whether the parameter is an input parameter or an output parameter, and the position of the parameter in the method signature.

  • When CustomAttributeData is used, information about custom features is found only when the application domain is reflected in context. CustomAttributeData enables you to check features without creating their instances.

 

The System. Reflection. Emit namespace class provides a dedicated form of Reflection that enables you to generate types at runtime.

You can also use reflection to create an application called a type browser, which enables users to select types and view information about these types.

Reflection has other purposes. Compilers in languages such as JScript use reflection to construct symbol tables. The classes in the System. Runtime. Serialization namespace use reflection to access data and determine which fields to save. Classes in the System. Runtime. Remoting namespace indirectly use Reflection Through serialization.

 

Runtime type in reflection

Reflection provides classes (such as Type and MethodInfo) to represent types, members, parameters, and other code entities. However, when using reflection, you do not directly use these classes, most of which are abstract. Instead, you use the type provided by the Common Language Runtime (CLR.

For example, use C #TypeofWhen the operator obtains the Type object, the object is actuallyRuntimeType.RuntimeTypeDerived from Type and provides implementation of all abstract methods.

These runtime classes areInternal. They are not recorded separately from their base classes because their behavior is described by the Base Class documentation.

 

 

[Reference] Microsoft official documentation

Related Article

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.