can anyone help to answer my questions?
I write the below function which receive the KProperty1<T, *> as a parameter:
fun <T> printProperty(instance: T, prop: KProperty1<T, *>) {
println("prop : ${prop.get(instance)}")
}
And I also define a Person class:
class Person(val name: String, var age: Int, private var address: String = "") {
// empty body
}
But when I write the test code, the compile of address property failed,
printProperty(person, Person::name) // Compile success
printProperty(person, Person::age) // Compile success
printProperty(person, Person::address) // Compile failed!!!
Although I know it is because the address field is private and cannot be accessed like Person::address
. But is there a way to construct Kproperty
for a private field to make it also can be used by the function?