What's the correct way to instantiate a class using the reflections (KClass) with primary constructor? Assuming we know the type and number of parameters.
Animesh Sahu
11/20/2020, 8:06 AM
Using the constructors:
Copy code
MyClass::class.constructors.first().call(5)
Copy code
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath
Following error occurs, do we need to add dependency to the kotlin-reflect for this? Is there any other way, in the standard libarary itself.
v
Vampire
11/20/2020, 8:32 AM
If you want to use reflection, you need the code for it. I don't think there is another way.
a
Animesh Sahu
11/20/2020, 8:33 AM
Though I found,
::MyClass
actually gives primary constructor reference, so it works 😛