开发者

grails how to delete property from object

suppose I have MyClass domain class:

class MyClass {
  String prop1
  String prop2
  String prop3
}

I wonder is there any way to delete for example prop1 property from M开发者_Go百科yClass object ?


The only way to actually delete the property is to remove it from the source file. However, you can make attempts to access the property exhibit the same behaviour as an attempt to access a non-existent property.

class MyClass {

  String prop1
  String prop2
  String prop3
}

MyClass.metaClass {
  // Intercept attempts to get the property
  getProp1 = {-> throw new MissingPropertyException("can't get prop1")}
  // Intercept attempts to set the property
  setProp1 = {throw new MissingPropertyException("can't set prop1")}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜