Skip to content

Kotlin/JS 反射

Kotlin/JS 为 Kotlin 反射 API 提供了有限支持。API 中唯一支持的部分是:

类引用

::class 语法返回实例的类引用,或给定类型对应的类。在 Kotlin/JS 中,::class 表达式的值是 KClass 的精简实现,它仅支持:

除此之外,您可以使用 KClass.js 访问与该类对应的 JsClass 实例。JsClass 实例本身是对构造函数的引用。这可用于与期望构造函数引用的 JS 函数进行互操作。

KType 和 typeof()

typeof() 函数为给定类型构造一个 KType 实例。KType API 在 Kotlin/JS 中获得完全支持,Java 特有部分除外。

KClass 和 createInstance()

来自 KClass 接口的 createInstance() 函数创建一个指定类的新实例,这对于获取 Kotlin 类的运行时引用非常有用。

示例

以下是 Kotlin/JS 中反射用法的一个示例。

kotlin
open class Shape
class Rectangle : Shape()

inline fun <reified T> accessReifiedTypeArg() =
    println(typeOf<T>().toString())

fun main() {
    val s = Shape()
    val r = Rectangle()

    println(r::class.simpleName) // 输出 "Rectangle"
    println(Shape::class.simpleName) // 输出 "Shape"
    println(Shape::class.js.name) // 输出 "Shape"

    println(Shape::class.isInstance(r)) // 输出 "true"
    println(Rectangle::class.isInstance(s)) // 输出 "false"
    val rShape = Shape::class.cast(r) // 将 Rectangle "r" 转换为 Shape

    accessReifiedTypeArg<Rectangle>() // 通过 typeOf() 访问类型。输出 "Rectangle"
}