gpt4 book ai didi

kotlin - 通过反射查找可为空的属性

转载 作者:IT老高 更新时间:2023-10-28 13:47:16 25 4
gpt4 key购买 nike

有没有办法列出允许返回空值的对象的所有属性?

val cls = javaClass<T>().kotlin

for(property in cls.properties) {
if(property.accessible) {
//Is it nullable?

}
}

最佳答案

您正在寻找的 API 是在最新的 Kotlin 版本 (0.13.213+) 中引入的。您现在可以获取属性的类型并查看它是否在源代码中被标记为可为空:

val property = ...
if (property.returnType.isMarkedNullable) {
...
}

关于kotlin - 通过反射查找可为空的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com