如何获取数据类的相对类名称
假设我们有这样的结构:
data class MyClass(
val inner: MyInnerClass
) {
data class MyInnerClass(
val foo: String
)
}
如何获得相对类名称myclass.myinnerclass
?我已经有那个班级的ktype。而且,如果我使用调试器看这个名称:
我找不到如何使用代码访问它。
Suppose we have a structure like this:
data class MyClass(
val inner: MyInnerClass
) {
data class MyInnerClass(
val foo: String
)
}
How can I get the relative class name MyClass.MyInnerClass
? I already have a KType of that class. And if I look at this with the debugger I can find the name:
I could not find out how to access this with code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用Kotlin反射伪像作为依赖性,这是可能的。
httpps://kotlinlang.org/docs/docs/reflection/reflection.html#jvm-dependence
在我的Maven项目中,我添加了一个新的依赖性:
我没有为此使用Gradle,但我认为您会使用
它。
我在名为
innerClass
的软件包中进行了类似于您的数据类:然后我的main.kt看起来像这样:
当我运行程序时,它会输出:
使用反射(和一个小字符串操作)我可以获取Kotlin类,并打印出班级的相对名称。
让我知道您是否遇到任何问题。
This is possible using the kotlin-reflect artifact as a dependency.
https://kotlinlang.org/docs/reflection.html#jvm-dependency
In my Maven project I add a new dependency:
I didn't use Gradle for this, but I think you'd use
to include it.
I made a data class similar to yours in a package named
innerclass
:Then my Main.kt looks like this:
When I run the program, it outputs:
Using reflection (and a little string manipulation) I can get the Kotlin class as well as print out the relative name of the class.
Let me know if you hit any issues.