Reflection Basics
Inspect types at runtime.
What Is Reflection?
Reflection is the ability of a program to inspect and manipulate its own structure at runtime: classes, properties, functions, and annotations.
Kotlin reflection lives in the kotlin.reflect package and centers on KClass.
Getting a KClass
Use the ::class syntax on a type or an instance to obtain its KClass object.
class Robot
fun main() {
val fromType = Robot::class
val fromInstance = Robot()::class
println(fromType.simpleName)
println(fromInstance.simpleName)
}All lessons in this course
- Using Annotations
- Defining Annotations
- Reflection Basics
- Practical Reflection