在 Kotlin 中遍历单例属性的正确方法是什么?

What's the correct way to iterate through properties of a singleton in Kotlin?

如标​​题所示,我想遍历单例对象的属性。我尝试使用 kotlin-reflect,因为我目前找不到其他方法。

object Test {
    const val a = "String"
    const val b = "Another String"
}

Test::class.declaredMemberProperties.forEach {
    println("${it.name}: ${it.get(Test)}")
}

但不幸的是,这会导致以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Callable expects 0 arguments, but 1 were provided.
...
at com.example.MainKt.main(main.kt:25)  // same line as println statement
...

似乎 get() 功能有问题(name 属性 解决得很好)。有没有更好的方法(也许没有反射)或一些解决方案来访问单例中的那些预编译常量?

看起来像是反射库中的问题。

您可以删除 const 修饰符,或使用以下解决方法:

Test::class.members.filterIsInstance<KProperty<*>>().forEach {
    println("${it.name}: ${it.call()}")
}

对于此用例,您可能根本不需要 kotlin-reflect。

object Test {
    const val a = "String"
    const val b = "Another String"
}

fun main() {
    Test::class.java
        .declaredFields
        .filter { it.name != "INSTANCE" }
        .map { "${it.name} = \"${it.get(Test)}\"" }
        .forEach(::println)
}
a = "String"
b = "Another String"

Link 到 Kotlin 游乐场:https://pl.kotl.in/bEgfrZk9k

对于 const val getter,您不需要传递接收者对象,因为它们是 compile-time 常量,而不是运行时常量。这就是编译器为您提供此错误的原因:Callable expects 0 arguments。使用此代码它适用于 const 和 non-const:

object Test {
    const val a = "String"
    val b = "Another String"
}

fun main() {
    Test::class.memberProperties.forEach {
        if (it.isConst) {
            println("const ${it.name}: ${it.getter.call()}")
        } else {
            println("${it.name}: ${it.getter.call(Test)}")
        }
    }
}