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)

Reflection (computer programming) -反射-自身结构信息的更多相关文章

  1. 类型检查和鸭子类型 Duck typing in computer programming is an application of the duck test 鸭子测试 鸭子类型 指示编译器将类的类型检查安排在运行时而不是编译时 type checking can be specified to occur at run time rather than compile time.

    Go所提供的面向对象功能十分简洁,但却兼具了类型检查和鸭子类型两者的有点,这是何等优秀的设计啊! Duck typing in computer programming is an applicati ...

  2. c#反射机制学习和利用反射获取类型信息

    反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...

  3. C#反射 获取程序集信息和通过类名创建类实例(转载)

    C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...

  4. 23 The Laws of Reflection 反射定律:反射包的基本原理

    The Laws of Reflection  反射定律:反射包的基本原理 6 September 2011 Introduction 介绍 Reflection in computing is th ...

  5. C#反射调用 异常信息:Ambiguous match found.

    异常信息(异常类型:System.Reflection.AmbiguousMatchException)异常提示:Ambiguous match found.异常信息:Ambiguous match ...

  6. Postgresql 导出表结构信息

    项目用了Postgresql 数据库,项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份.查了一番资料,似乎没有多好的方法.dump方式导出的脚本太乱,没法直接写在word文档里.只能自己写s ...

  7. SQLServer查询所有库表结构信息

    1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

  8. c#通过oledb获取excel文件表结构信息

    这个问题来自论坛提问,同理可以获得access等数据库的表结构信息. using System; namespace ConsoleApplication11 { class Program { pu ...

  9. The Art of Computer Programming

    <计算机程序设计艺术>即<The Art of Computer Programming>是计算机领域里颠峰级的里程碑,加上国外人士对它的推崇,所以提起它的大名简直就象法律书籍 ...

随机推荐

  1. HTML 引入 CSS、JS 的三种方式

    描述部分按 CSS 来的,其实 JavaScript 也一样,具体区别看代码 外部样式表 当样式需要被应用到很多页面的时候,外部样式表将是理想的选择.使用外部样式表,你就可以通过更改一个文件来改变整个 ...

  2. "国学"能带给孩子什么?

       您是家长吗?   是!就点标题下方蓝色的 静心坊 三字,然后点关注!您就可以享受到我们为您提供的最新教育信息. 读国学,除大量识字之外,还能带给孩子什么呢? 男孩子读国学,国学能教会他做一个正直 ...

  3. atom安装与插件配置

    下载:https://atom.io 安装 必备插件安装步骤: File-->settings-->Install-->Install Packages下面的输入框中输入要安装的插件 ...

  4. Redis详解入门篇(转载)

    Redis详解入门篇(转载) [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 ...

  5. 洛谷P1231 教辅的组成 最大流

    裸题… Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vecto ...

  6. 路飞学城Python-Day80

    36-模板语法之继承 Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 blocks . ...

  7. Java 接口技术 Interface

    一.什么是接口技术(Interface): //举例中Comparable是一个接口,Employee是一个类 1.接口不是类,而是对类的一组描述,并不给出每个类的具体实现. 2.一个类可以实现多个接 ...

  8. nodejs-安装及卸载

    linux下安装node 1.编译的方式安装​ 1 2 3 4 5 6 7 wget http://nodejs.org/dist/node-latest.tar.gz tar zxvf node-l ...

  9. Vijos——T 1016 北京2008的挂钟 || 洛谷—— P1213 时钟

    https://www.luogu.org/problem/show?pid=1213 题目描述 考虑将如此安排在一个 3 x 3 行列中的九个时钟: 目标要找一个最小的移动顺序将所有的指针指向12点 ...

  10. DynaActionForm(动态ActionForm)的使用

    在struts中利用DynaActionForm(动态ActionForm)可以节省代码的编写. 1.在struts-config.xml中配置DynaActionForm:加入这个Form中有三个属 ...