Reflection (computer programming) -反射

computer sciencereflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.[1]

A language supporting reflection provides a number of features available at runtime that would otherwise be difficult to accomplish in a lower-level language. Some of these features are the abilities to:

  • Discover and modify source-code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
  • Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
  • Evaluate a string as if it were a source-code statement at runtime.
  • Create a new interpreter for the language‘s bytecode to give a new meaning or purpose for a programming construct.

These features can be implemented in different ways. In MOO, reflection forms a natural part of everyday programming idiom. When verbs (methods) are called, various variables such as verb (the name of the verb being called) and this (the object on which the verb is called) are populated to give the context of the call. Security is typically managed by accessing the caller stack programmatically: Since callers() is a list of the methods by which the current verb was eventually called, performing tests on callers()[1] (the command invoked by the original user) allows the verb to protect itself against unauthorised use.

Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program. In a compiled language that supports runtime creation of functions, such as Common Lisp, the runtime environment must include a compiler or an interpreter.

Reflection can be implemented for languages not having built-in reflection facilities by using a program transformation system to define automated source-code changes.

Uses[edit]

Reflection can be used for observing and modifying program execution at runtime. A reflection-oriented program component can monitor the execution of an enclosure of code and can modify itself according to a desired goal related to that enclosure. This is typically accomplished by dynamically assigning program code at runtime.

In object-oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.

Reflection can be used to adapt a given program to different situations dynamically. Reflection-oriented programming almost always requires additional knowledge, framework, relational mapping, and object relevance in order to take advantage of more generic code execution.

Reflection is often used as part of software testing, such as for the runtime creation/instantiation of mock objects.

Reflection is also a key strategy for metaprogramming.

In some object-oriented programming languages, such as C# and Java, reflection can be used to override member accessibility rules. For example, reflection makes it possible to change the value of a field marked "private" in a third-party library‘s class.

https://en.wikipedia.org/wiki/Reflection_(computer_programming)

原文地址:https://www.cnblogs.com/feng9exe/p/8277474.html

时间: 2024-10-30 03:19:50

Reflection (computer programming) -反射的相关文章

K老在拿图灵奖时的发言:Computer Programming as an Art

很多话说得很透彻,把一些觉比较精彩的摘抄一下.... It seems to me that if the authors I studied were writing today, they would agree with the following characterization: Science is knowledge which we understand so well that we can teach it to a computer; and if we don't ful

CCN2042 Computer Programming

CCN2042 Computer ProgrammingGroup Project – Cooking Game(Due: 23:59, 18 Apr 2019 (week 12 before Easter))Expected Learning Outcomes familiarise themselves with at least one high level language programming environment. develop a structured and documen

MTH5001: Introduction to Computer Programming

projectMarch 14, 20191 MTH5001: Introduction to Computer Programming 2018/191.1 Final Report Project: "Networks"1.1.1 Instructions:First, please type your name and student number into the Markdown cell below:Name:Student number:You must write yo

ISOM 3029 - Computer Programming Using C++

University of MacauFaculty Of Business AdministrationISOM 3029 - Computer Programming Using C++ 2019/2020 (First Semester)Assignment 1Due date: October 11, 2019 (before 5pm)Submitted to: Ms YunYun Zhou (GA) Instructions:?Read the Assignment Requireme

CS2313 Computer Programming

CS2313 Computer ProgrammingAssignment OneDeadline: 2019-Nov-16 11:59pmLate submission: 70% (within 24hrs after the deadline), 0% (after 24hrs)1. Problem descriptionA “lucky sequence” is the sequence of numbers where every number after the first twois

ios开发 Reflection(三) 利用反射自动绑值

反射的具体应用,自动绑值 获取属性列表 1 - (NSArray*)propertyKeys 2 { 3 unsigned int outCount, i; 4 objc_property_t *properties = class_copyPropertyList([self class], &outCount); 5 NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:outCount]; 6 for (i = 0;

Java RTTI机制与反射机制

1.1 什么是RTTI? 维基百科的定义:In computer programming, RTTI (Run-Time Type Information, or Run-Time Type Identification) refers to a C++ mechanism that exposes information about an object's data type at runtime. Run-time type information can apply to simple d

现代 C++ 编译时 结构体字段反射

基于 C++ 14 原生语法,不到 100 行代码:让编译器帮你写 JSON 序列化/反序列化代码,告别体力劳动.?? 本文不讨论完整的 C++ 反射技术,只讨论结构体 (struct) 的字段 (field) 反射,及其在序列化/反序列化代码生成上的应用. 正文开始于 [sec|静态反射] 部分,其他部分都是铺垫..可以略读... 背景(TL;DR) 很多人喜欢把程序员称为 码农,程序员也经常嘲讽自己每天都在 搬砖.这时候,大家会想:能否构造出一些 更好的工具,代替我们做那些无意义的 体力劳动

反射的用法和好处

今天我们就来说一说反射(Reflection ), 反射是什么呢?反射是动态获取程序集的元数据(metadata)的一种技术.反射是.NetFramework类库提供的帮助类,动态加载dll实现程序的可配置可扩展. 首先我们来看一个简单的实现反射的例子,我们先创建整个框架 IDAL里面是一个接口,接口里面有一个Query方法, using System; namespace Reflect.IDAL { public interface IHelper { void Query(); } } 假