I wrote an extension function for Any
type, that will retrieve object property value by its name. I want to be able to use it from everywhere in my project. Here is my extension function:
package com.example.core.utils.validation
import java.util.NoSuchElementException
import kotlin.reflect.full.memberProperties
fun Any.getFieldValue(fieldName: String): Any? {
try {
return this.javaClass.kotlin.memberProperties.first { it.name == fieldName }.get(this)
} catch (e: NoSuchElementException) {
throw NoSuchFieldException(fieldName)
}
}
Now I want to use it like this
package com.example.core
import com.example.core.utils.validation.*
class App {
val obj = object {
val field = "value"
}
val fieldValue = obj.getFieldValue("field")
}
But there is Unresolved reference error
How should I make my extension function global and import it anywhere?