Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of any class?

Tags:

kotlin

I am trying to make an extension function for logging. I want to send Any as a parameter and get its name. But can not find how to get name of Any class. Have any idea about it?

fun MainActivity.log(claz: Objects,any: Any){
    Log.d("Main", claz.name +  any.toString())
}
like image 580
Nurseyit Tursunkulov Avatar asked Dec 24 '22 02:12

Nurseyit Tursunkulov


2 Answers

val c = String::class
println(c.simpleName)

$ String
  • Class References
  • KClass
like image 85
elken Avatar answered Jan 18 '23 12:01

elken


Use yourClassInstance::class.simpleName:

fun MainActivity.log(claz: Objects, any: Any){
    Log.d("Main", "${claz::class.simpleName} ${any::class.simpleName}")
}
like image 26
Sergey Avatar answered Jan 18 '23 14:01

Sergey