vendredi 18 novembre 2016

Creating a new instance of a KClass

I have a Kotlin class whose primary (and only) constructor is empty.

I have a reference to this class:

val kClass: KClass<MyClass> = MyClass::class

How do I create an instance of this class using reflection?

In Java I would do myClass.newInstance() but it seems in Kotlin I need to find the constructor first:

kClass.constructors.first().call()

I have seen mention of primaryConstructor in some bug reports but it's not showing up in my IDE.





Aucun commentaire:

Enregistrer un commentaire