Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

question from:https://stackoverflow.com/questions/34340450/how-to-get-a-kotlin-kclass-from-a-package-class-name-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
148 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...