[英]In Scala, How to get the returned TypeTag of a class method?
我有一堂课:
package org.apache.project
class Foo {
def bar: List[Bar] = ...
}
这是一种Scala反射方式,可让我从className“ org.apache.project.Foo”和方法名称“ bar”获取typeOf [List [Bar]]?
非常感谢你的帮助!
是的,如果您将Foo
作为Type
,则也可以找到方法bar
的返回类型也就是Type
。
import scala.reflect.runtime.universe._
typeOf[Foo] // Get the type of Foo
.decls // Get Foo's declared members
.find(_.name.toString == "bar") // Find the member named "bar" as a symbol
.head // Unsafely access it for now, use Option and map under normal conditions
.asMethod // Coerce to a method symbol
.returnType // Access the return type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.