【问题标题】:Kotlin Get properties from generic typeKotlin 从泛型类型中获取属性
【发布时间】:2020-04-13 09:35:26
【问题描述】:

我可以在 C# 中做这样的事情

PropertyInfo propertyInfo = typeof(TEntity).GetProperties()

如何在 Kotlin 中做同样的事情

【问题讨论】:

  • 你的意思是KClass反射的属性?

标签: generics kotlin reflection


【解决方案1】:

JVM 在运行时遭受类型擦除。

您不能简单地做到这一点,但 Kotlin 为 inline 函数提供了 reified 关键字以利用

inline fun <reified T: Any> takeKClass() {
    val reflection: KClass<T> = T::class
    ...
}

但请记住,内联函数在编译时嵌入到调用站点中,但这是 Kotlin 提供的一个很好的功能。

【讨论】:

猜你喜欢
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2018-07-26
  • 2015-02-15
  • 2012-12-17
  • 1970-01-01
相关资源
最近更新 更多