Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime. Use this tag for programming features that need to reason about the code itself. For questions about rendering visual reflections, use the Reflection-Rendering tag instead.

Quoting the Wikipedia article on reflection:

In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior 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.

history | excerpt history