Reflection - How to box a class/object instance?

Monkey Forums/Monkey Beginners/Reflection - How to box a class/object instance?

PoliteProgrammer(Posted 2014) [#1]
Hello everyone, I have (hopefully) a simple question; how do I box a class/object instance? What I mean is, to box an int, I'd use BoxInt(), and for a string I'd use BoxString(), but what if I want to box an instance of one of my own classes? There doesn't seem to be a BoxObject() function.


Goodlookinguy(Posted 2014) [#2]
Just cast them to an object?
Local obj:Object = Object(New MyType())



Jesse(Posted 2014) [#3]
you don't need to down cast it. Every object created extends from the "Object" class.
this works:
local obj:Object = new MyType()


you do need to up cast it though:
Local this:MyType = MyType(obj)



PoliteProgrammer(Posted 2014) [#4]
Thanks everyone, what I was missing was that the point of the BoxInt() etc methods is to package the built-in types as objects. So if you already have an object, the object can just be used directly.


AdamRedwoods(Posted 2014) [#5]
you can use "Property" if you need getters/setters.