Cast with GetType()

后端 未结 5 1905
无人及你
无人及你 2020-12-15 03:13

Is it possible to cast an object to the type returned from GetType()? I\'d like a generic method that can accept an object (for anonymous types) but then return

5条回答
  •  情歌与酒
    2020-12-15 03:53

    You can use getClass() which returns a Class object then use the cast method in the Class object to cast the object to an object of that Class's type, like so:

    myObj.getClass().cast(myObj)

提交回复
热议问题