0

我有一堂课:

package org.apache.project

class Foo {

def bar: List[Bar] = ...
}

它是 scala 反射中的一种方法,允许我从类名“org.apache.project.Foo”和方法名“bar”中获取 typeOf[List[Bar]]?

非常感谢你的帮助!

4

1 回答 1

1

是的,如果你有Fooas a Type,你也可以找到方法的返回类型baras a 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
于 2016-05-27T23:56:44.637 回答