[英]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.