开发者

cast object to specific class by string

how can i dynamically cast oject to a specific class which is given as a string. e.g.

def a = (ClassA) testService.getObject(xmlString)

i would like to do something like this

开发者_如何转开发
(grailsApplication.getClassForName(classString)) testService.getObject(xmlString)

but the groovy compiler does not like this way of cast operation.


you could do

Class myClass = grailsApplication.getClassForName(classString)
myClass.cast testService.getObject(xmlString)

but I'm not sure how groovy this way is - it's really just Java way..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜