mardi 19 janvier 2021

How to get a function reference using reflection in Kotlin

Let's suppose I have an instance of a class X with a method Y of which we will know the name only at runtime, how can I get a reference to it using reflection?

Something like:

class X{

  fun Y(){
    
  }

}

I want to able to store the method Y in a variable and call it when needed.

I tried with X::class.java::getMethod('Y').kotlinFunction but then I will need to have an instance of such method to be able to call it, so it doesn't make any sense





Aucun commentaire:

Enregistrer un commentaire