Reflection (computer programming) -反射-自身结构信息
n computer science, reflection 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) -反射-自身结构信息的更多相关文章
- 类型检查和鸭子类型 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 ...
- c#反射机制学习和利用反射获取类型信息
反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得每个成员的 ...
- C#反射 获取程序集信息和通过类名创建类实例(转载)
C#反射获取程序集信息和通过类名创建类实例 . System.Reflection 命名空间:包含通过检查托管代码中程序集.模块.成员.参数和其他实体的元数据来检索其相关信息的类型. Assembly ...
- 23 The Laws of Reflection 反射定律:反射包的基本原理
The Laws of Reflection 反射定律:反射包的基本原理 6 September 2011 Introduction 介绍 Reflection in computing is th ...
- C#反射调用 异常信息:Ambiguous match found.
异常信息(异常类型:System.Reflection.AmbiguousMatchException)异常提示:Ambiguous match found.异常信息:Ambiguous match ...
- Postgresql 导出表结构信息
项目用了Postgresql 数据库,项目组要出表结构的文档,手写太麻烦,想用slq脚本导出一份.查了一番资料,似乎没有多好的方法.dump方式导出的脚本太乱,没法直接写在word文档里.只能自己写s ...
- SQLServer查询所有库表结构信息
1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...
- c#通过oledb获取excel文件表结构信息
这个问题来自论坛提问,同理可以获得access等数据库的表结构信息. using System; namespace ConsoleApplication11 { class Program { pu ...
- The Art of Computer Programming
<计算机程序设计艺术>即<The Art of Computer Programming>是计算机领域里颠峰级的里程碑,加上国外人士对它的推崇,所以提起它的大名简直就象法律书籍 ...
随机推荐
- 什么是JSON ?
JSON 实例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- sql 注入例子及防止
一.什么是sql注入? 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令 二.sql例子 1.数字注入 假设在项目中,在 ...
- webpack学习笔记(3)--webpack.config.js
module 参数 使用下面的实例来说明 module.exports = { module: { rules: [ { test: /\.css$/, use: 'css-loader' }, { ...
- linux基础正则
1.^word #匹配word开头的内容,vi/vim编辑器里^代表一行的开头. 2.word$ #匹配以word结尾的内容,vi/vim编辑器里$代表一行的结尾. 3.^$ #表示空行 4.. #代 ...
- 搞定PHP面试 - 深入了解引用
1. 什么是引用 在 PHP 中引用是指用不同的名字访问同一个变量内容.PHP 中的变量名和变量内容是不一样的, 因此同样的内容可以有不同的名字.最接近的比喻是 Unix 的文件名和文件本身--变量名 ...
- js:Array对象常用方法介绍
前言 在js中,数组作为一个特殊的对象.是我们常用的数据格式.今天就来梳理一下常用的数组方法. 1.基础 几种基础的就简单介绍一下:创建数组 var arr1 = new Array(); //括号可 ...
- 使用 python 读写中文json
读写中文json ) 输出中文的json. 通过使用 ensure_ascii=False,输出原有的语言文字.indent參数是缩进数量. 更改写文件格式 将上一步导出的 string 直接写文 ...
- MQ发送定时消息
通过延时发送来发送定时消息. RocketMQ只支持固定精度时间的延时消息发送:1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h 若 ...
- Gradle:Gradle入门
一.安装Gradle 1.首先确保你安装的JDK1.5或以上版本号. C:\Users\chengxiang.peng.QUNARSERVERS>java -version java ver ...
- mysqli简介
mysqli简介 PHP MySQLi 简介 PHP MySQLi = PHP MySQL Improved! MySQLi 函数允许您访问 MySQL 数据库服务器. 注释:MySQLi 扩展被设计 ...