Another way I found is to declare a local class or an anonymous object inside a top level function and to get its enclosingClass
:
val topLevelClass = object : Any() {}.javaClass.enclosingClass
Note: to work, this declaration should be placed on top level or inside a top-level function.
Then you can use the topLevelClass
as a Class<out Any>
:
fun main(args: Array<String>) {
println(topLevelClass) // class MyFileNameKt
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…