Skip to content

Instantly share code, notes, and snippets.

@albodelu
Forked from sanogueralorenzo/Navigation.kt
Created November 16, 2019 11:56
Show Gist options
  • Save albodelu/c2cf955dadc48d19234397fd03c3015d to your computer and use it in GitHub Desktop.
Save albodelu/c2cf955dadc48d19234397fd03c3015d to your computer and use it in GitHub Desktop.
private val classMap = mutableMapOf<String, Class<*>>()
private inline fun <reified T : Any> Any.castOrNull() = this as? T
internal fun <T> loadClassOrNull(className: String): Class<out T>? {
return classMap.getOrPut(className) {
try {
Class.forName(className)
} catch (e: ClassNotFoundException) {
return null
}
}.castOrNull()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment