我正在尝试匹配String?
Kotlin 反射练习中可为空的类型:
data class Test(a: String, b: String?)
val test = Test("1", "2")
val properties = test::class.declaredMemberProperties
val propertyNames = properties.joinToString(",") {
when (it.returnType) {
String?::class.createType() -> "string?"
String::class.createType() -> "string"
else -> throw Exception()
}
}
唉,它因错误而失败,Type in a class literal must not be nullable
, for String?::class
。