how can i iterate through all properties of a data...
# announcements
l
how can i iterate through all properties of a data class instance with reflection so i can get both the name and the value of the property? I tried
Copy code
fun main() {
	val thing = MyClass(42, "69")

	thing::class.memberProperties.forEach {
		println(it.name)
		println(it.get())
	}
}

data class MyClass(val x: Int, val y: String)
but
get()
requires a parameter of type
Nothing
which i dont understand
e
change
it.get()
->
it.getter.call(thing)
Not sure how (if)
it.get()
is supposed to be used in this case
d
It needs to be bound for
get()
to be used I think. Like
this::propName
, is bound to this.
l
that makes sense. ty
a
You can use the
.get()
method if you start from a class literal (e.g.
MyClass::class
)
141 Views