get array field using reflection

BlitzMax Forums/BlitzMax Programming/get array field using reflection

plash(Posted 2008) [#1]
How can I get a field as an array (using reflection)?

EDIT: ie.
Type MyType
    Field array:int[]
End Type

..get typeid and field object...

Local array:int[] = fld.GetAsIntArray()

Print array[0]



Azathoth(Posted 2008) [#2]
Is reflection necessary?

Type MyType
    Field array:Int[]

	Method New() 
		array = New Int[10]
		
		For Local i = 0 Until array.length
			array[i] = i
		Next
	EndMethod
End Type

Local ab:MyType=New MyType

Local array:Int[] = ab.array

Print array[5]



Otus(Posted 2008) [#3]
When you don't know the type it is.

There's no way to get the array directly - it can be of any dimensions and any type, after all. You should reconstruct it element by element through typeid.ArrayLength and typeid.GetArrayElement, where typeid is TTypeId.ForObject(fld).

However, if anyone knows how to handle (real) multi-dimensional arrays, please share your knowledge.


plash(Posted 2008) [#4]
Is reflection necessary?
Yes it is, notice how I specifically asked how to do it using reflection.

Figured it out (I think):
Local fld:TField
Local fldtype:TTypeID = fld.TypeID()

Print String(fldtype.GetArrayElement(fld, 0))