1
我想遍历其中一个类的所有字段,筛选注释的字段,然后检查该字段是否有一个特定的类型。
我找到的只是field.returnType.isSubtype(other: KType)
,但我不知道如何获得我其他班级的KType
。Kotlin Reflection - 检查属性是否有类型
这是到目前为止我的代码:
target.declaredMemberProperties.forEach {
if (it.findAnnotation<FromOwner>() != null) {
if (/* it.returnType is Component <- Here I need some working check */) {
// do stuff
} else {
// do ther stuff
}
}
}
这是否意味着在伴随对象中有'val componentType by lazy {Component :: class.createType()}'是一个好主意? – danielspaniol
@Exhauzt,我想基准你的特定用例应该在这里给出最好的答案。 – hotkey