Grammin
Grammin

Reputation: 12205

How can I access a domain property using the property name?

I have a domain object with a property called date:

class Item implements Comparable{
  Date date
}

How can I access that date doing something like:

Item.list().each{
  Date d = it.get("date")
}

I know that I could do Date d = it.date but I want to be able to generically pick a property from my domain object and access it without using .property.

Upvotes: 1

Views: 113

Answers (2)

cawka
cawka

Reputation: 437

This should also work:

String propertyName = 'date'

Item.list().each {
    Date d = it."$propertyName"
}

Upvotes: 3

MKB
MKB

Reputation: 7619

Try this..,.

Item.list().each {
    Date d = it.properties.get("date")
}

or

Item.list().each {
    Date d = it.getProperty("date")
}

Upvotes: 2

Related Questions