How to get the name of a variable in Kotlin?

后端 未结 3 1604
孤城傲影
孤城傲影 2020-12-20 19:55

I have a Kotlin class in my application with a lot of attributes, what I want to build is a method that stores the variable name in a dictionary. The dictionary looks like t

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 20:16

    Use memberProperties to get the names of the class attributes and others properties. For instance:

    YourClass::class.memberProperties.map {
     println(it.name)
     println(it.returnType)
    }
    

提交回复
热议问题