How to get a Kotlin KClass from a package class name string?

后端 未结 1 1674
余生分开走
余生分开走 2021-01-07 16:05

If I have a string like \"mypackage.MyClass\", how can I get the corresponding KClass at runtime (from the JVM)?

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 16:44

    You can use Java's method of getting a Class instance Class.forName and then convert it to a KClass using the .kotlin extension property. The code then looks like this:

    val kClass = Class.forName("mypackage.MyClass").kotlin
    

    A more direct way may be added at some point. The issue is located here

    0 讨论(0)
提交回复
热议问题