How to get instance of property?

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?

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" }

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

1 Like

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

1 Like