Search code examples
groovydynamicgetproperty

Hows the syntax to getProperty dynamically?


Is this possible and if, how?

item.foo = "moo"

def x = "foo"
item.[x] = "moo"

Solution

  • I'm not sure I get your question, but...

    def item = {
        def foo
    }
    def x = "foo"
    item[x] = "moo"
    
    assert item[x] == "moo"
    assert item.foo == "moo"