Scala 在Guice中非静态绑定类

Scala 在Guice中非静态绑定类,scala,guice,Scala,Guice,在我的Scala项目中,我使用Guice绑定了许多以三元组形式出现的类,比如: bind[Foo1Component].to[Foo1] bind[Foo2Component].to[Foo2] bind[Foo3Component].to[Foo3] // And so on for Bar, Baz, etc. 我想如果有一个函数,我将字符串“Foo”传递给它,并让它执行所有三个绑定,那就太好了。我能得到的最接近的结果是: def bindAll(name: String): Unit =

在我的Scala项目中,我使用Guice绑定了许多以三元组形式出现的类,比如:

bind[Foo1Component].to[Foo1]
bind[Foo2Component].to[Foo2]
bind[Foo3Component].to[Foo3]
// And so on for Bar, Baz, etc.
我想如果有一个函数,我将字符串
“Foo”
传递给它,并让它执行所有三个绑定,那就太好了。我能得到的最接近的结果是:

def bindAll(name: String): Unit = {
  bind(Class.forName(name + "1Component")).to(Class.forName(name + "1"))
  // likewise for 2 and 3, or do it in a loop
}
…但这给了我一条很长的错误消息,大意是编译器无法找出我想要哪个重载的
to
方法。由于
bind(classOf[Foo1Component])to(classOf[Foo1])
有效,我认为问题在于
Class.forName
返回一个
Class[\u]
而不是一个带有适当类型参数的
Class


有没有办法像这样动态绑定类?

我不认为这是可能的,检查相关的我不认为这是可能的,检查相关的