lundi 22 octobre 2018

Getting member variables through reflection in Kotlin

If I have a class in Kotlin:

class Foo{

var x= null
var y=null

}

I want to check which of these members have been set at runtime through reflection. I can iterate over them and check for null in Java.

Foo foo= new Foo();

//this gives me the value of foo.x
Foo.class.getDeclaredField("x").get(foo);

How can I do the same in Kotlin/Native? I know I can achieve this in Android by

Foo::class.java.getDeclaredField("x").get(foo)

But this doesn't work in native environment.





Aucun commentaire:

Enregistrer un commentaire