Reflection field - >list?
Monkey Forums/Monkey Programming/Reflection field - >list?
| ||
| when i cast a list to generic <Object> it is always NULL but if I cast straight to List<character> it works When I am saving or loading a game I wont know what type of data the lists contain So, how do I deal with a list when I dont know what type it will contain?
Strict
Import reflection
Import mojo
Class trev Extends App
Method OnCreate:Int()
world_map.inst=New world_map
world_map.inst.character_list=New List<character>
Local c:character=New character
c.name="TREV"
world_map.inst.character_list.AddLast(c)
SetUpdateRate 30
Local clas:=GetClass(world_map.inst )
Print "class name "+clas.Name
For Local info:=Eachin clas.GetFields()
Print " Field "+info.Name
If info.Type<>Null
Print "type"+ info.Type.Name
If info.Type.Name.Find("monkey.list.List")=0
Print "Found a List!"
Local l:List<Object>=List<Object>(info.GetValue(world_map.inst))
If l<>Null
For Local o:Object=Eachin l
Print "found something on the list
Next
Else
Print "the list is null!"
Endif
Endif
Endif
Next
Return 0
End Method
Method OnRender:Int()
Return 0
End Method
Method OnUpdate:Int()
Return 0
End Method
End Class
Function Main:Int()
New trev
Return 0
End Function
Class character
Field name$
End Class
Class world_map
Global inst:world_map
Field character_list:List<character>
Field num:Int
Field float_num:Float
Field my_String:String
End Class
|
| ||
| Hi, This is a limitation of the generics/reflection system. List<Object> and List<Thing> are 2 entirely separate classes - you can't cast a List<Thing> to a List<Object>. Also, reflection can't 'create' a generic class that didn't already exist when the program was compiled. To do so, it would need to be able to compile/build/link an entirely new List<> class on the fly. You should be able to use reflection to traverse an unknown type of list though - ie: find the 'ObjectEnumerator' method in the List and use that. It would probably also be nice if there was a common 'Enumerable' interface or similar... |
| ||
| You should be able to use reflection to traverse an unknown type of list though - ie: find the 'ObjectEnumerator' method in the List and use that. When you say 'you should be able to' you mean Monkey should support that. You dont mean its already possible do you? 'Enumerable' would be very good as it might allow me to traverse a list, stack,map or set without having to write a function for each data holder? |
| ||
| Hi, Ok, got this working but it involved another fix - not all generic instantiation members were being reflected. Will have a think about this, there's hopefully a better way to do it! |