【问题标题】:Is there a way of passing all the defined variables to a GroovyShell?有没有办法将所有定义的变量传递给 GroovyShell?
【发布时间】:2023-04-04 11:52:01
【问题描述】:

现在,当我尝试在 Groovy 中评估一段代码时,我必须这样做:

new GroovyShell(new Binding([var1:var1])).evaluate(line)

当您定义了很多变量时,这可能会很糟糕。有没有更好的方法来做到这一点?是否有类似 Python 的 locals 之类的东西,或者类似的东西列出了所有声明的变量?

【问题讨论】:

  • 您需要在 groovy shell 实例中声明的变量?
  • 我需要访问在实例化 groovy shell 之前声明的变量。

标签: binding groovy eval


【解决方案1】:

我还没有测试过这个......但它可能会起作用:

new GroovyShell(this.binding).evaluate(line)

或者这个:

new GroovyShell(new Binding(this.binding.variables)).evaluate(line)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2021-11-26
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多