mercredi 17 mars 2021

(Kotlin) How can i execute lambda () -> Unit like function parameter through reflection

i have code

object Boo {
    val lambda: () -> Unit = { print("lambda") }
    operator fun invoke(init: Foo.() -> Unit){
    }
}

And I don't understand how can i execute init in reflection.


For example, I can execute lambda class member like this

    ((this.javaClass.kotlin
.declaredMemberProperties as List<*>)[0] asKProperty1<Boo,*>).invoke(this) //works

so init i get like parameter invoke function only

    val kparameter: KParameter = this.javaClass.kotlin.functions
.first { it.name == "invoke" }.parameters[0]

but i can't do anything with it!

Can anyone help me with this question?





Aucun commentaire:

Enregistrer un commentaire