[ACCEPTED]-Kotlin Reflection - Check if property has type-kotlin
Accepted answer
There are at least two solutions here:
Get 7 the
KClass<*>
ofit.returnType
using.jvmErasure
, then check the subtype 6 relationship for theKClass
es:it.returnType.jvmErasure.isSubclassOf(Component::class)
Since Kotlin 1.1, you 5 can construct the
KType
from theKClass
token using 4.createType()
(check its optional parameters: you can 3 use them to provide nullability info, type 2 arguments and annotations), and then check 1 the subtype as you suggested:it.returnType.isSubtypeOf(Component::class.createType())
Creating the type on every iteration may introduce performance issues, make sure you cache it if you need it often.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.