开发者

cast object to specific class by string

开发者 https://www.devze.com 2023-03-04 15:32 出处:网络
how can i dynamically cast oject to a specific class which is given as a string. e.g. def a = (ClassA) testService.getObject(xmlString)

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

精彩评论

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