如何使用元组初始化case类来自反射

时间:2016-10-13 02:39:40

标签: scala

我想使用元组初始化case类来自反射,但它不起作用,请帮助我:

case class kof(argv1:String,argv2:String)

var kof=Class.forName("kof")
val ru = scala.reflect.runtime.universe
val m = ru.runtimeMirror(getClass.getClassLoader)
val classPerson = ru.typeOf[kof].typeSymbol.asClass
val cm = m.reflectClass(classPerson)
val ctor = ru.typeOf[kof].declaration(ru.nme.CONSTRUCTOR).asMethod
val ctorm = cm.reflectConstructor(ctor)
var sss=("22","ad")
val p = ctorm.tupled(sss)<!- here what I want to ask, please answer me as soon as possible, thank you!->
print (p)

1 个答案:

答案 0 :(得分:1)

约:

scala> ctorm(sss.productIterator.toSeq: _*)
res3: Any = kof(22,ad)