How to access a field's value via reflection (Scala 2.8)

前端 未结 5 1979
北海茫月
北海茫月 2021-02-08 00:18

Consider the following code:

class Foo(var name: String = \"bar\")

Now i try to get the value and the correct type of it via reflection:

<
5条回答
  •  灰色年华
    2021-02-08 00:31

    foo.getClass.getDeclaredField("name").getString(foo)
    

    should work if you want to avoid asInstanceOf. get* is available for various types

提交回复
热议问题