Implementing a Type Field copy with Reflection?

BlitzMax Forums/BlitzMax Programming/Implementing a Type Field copy with Reflection?

nomen: congratulation to your active null reference creation.
Copy of memory does not change the reference count -> zack and bang if the original object ever goes out of scope



here is a function I wrote as proof of concept for the german boards:

SuperStrict

Type TCloneTest
   Field x:Int
   Field y:Int
End Type

Function DoClone:Object(obj:Object)
   Local typ:TTypeId    = TTypeId.ForObject(obj)
   Local clone:Object   = typ.newObject()
   
   For Local t:TField = EachIn typ.EnumFields()
      t.set(clone,t.get(obj))
   Next
   Return clone
End Function

Local original:TCloneTest = New TCloneTest
original.x = 10
original.y = 17


Local clone:TCloneTest = TCloneTest(DoClone(original))

Print "Clone: " + clone.x + ", " + clone.y