How to get instance of property?


#1

Given a property reference ref like

val ref = instance::prop

where instance is the instance of some class containing a properly named prop, how can I get back instance from a given ref again?


#2

In simple cases you can use this: :wink:

MyType::prop

If it’s not that straigthforward and you want to use reflection on any instance of KClass<T> then you have to resort to something this:

classInstance.{propertyType}Properties.[first|find] { it.name == "prop" }

#3

If you’re targeting JVM, this seems to work: (ref as CallableReference).boundReceiver


#4

There is already an issue for that: https://youtrack.jetbrains.com/issue/KT-16818