简体   繁体   English

如何获取 .kt 文件中所有类的列表

[英]How to get a list of all classes in a .kt file

Is it possible to grab a list of classes inside a .kt file?是否可以在 .kt 文件中获取类列表? It appears that KDeclarationContainer is the class I am looking for, but I am unsure of how to use it.看起来 KDeclarationContainer 是我正在寻找的类,但我不确定如何使用它。

EDIT: I am using Kotlin Reflection.编辑:我正在使用 Kotlin 反射。

It's not possible to obtain all classes which were declared in a specific .kt source file at runtime via reflection API, in much the same way as it's impossible to get all Java classes in a .java source file with Java reflection API.不可能在运行时通过反射 API 获取在特定 .kt 源文件中声明的所有类,这与使用 Java 反射 API 获取 .java 源文件中的所有 Java 类几乎相同。 This information is simply not available at runtime.此信息在运行时根本不可用。

What is your use case?你的用例是什么? I think if you're relying on something like that, your best workaround is to declare your classes inside one top-level class, and call KClass#nestedClasses to get them all at runtime.我认为如果您依赖类似的东西,最好的解决方法是在一个顶级类中声明您的类,并在运行时调用KClass#nestedClasses来获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM