How do I cast to an Interface / Class from Kotlin Class from String?

Viewed 4930

I get a class with Class.forName("classname").kotlin.

I know that the class implements an Interface, which is extremly simple (if you have a solution that is different from what I think, it would work great too)

interface Runner {
    fun run(): String
}

When I try to cast my class, to use the function, like this

val result = (Class.forName("classname").kotlin as Runner).run()

I get the following error:

kotlin.reflect.jvm.internal.KClassImpl cannot be cast to Runner

Is there even a way to make this work? I also tried it with Class.forName("classname").kotlin.functions and then tried to execute the function from there, but I did not have success with that.

If you know the Java solution to this problem (calling a function from the class name) it may be nice to hear that too.

1 Answers
Related