October 20, 2021, 11:09:19

Author Topic: Extracting Globals from Type using Reflection  (Read 267 times)

Offline Scaremonger

  • Full Member
  • ***
  • Posts: 233
    • ITSpeedway - Ramblings of a geek!
Extracting Globals from Type using Reflection
« on: January 31, 2021, 11:14:52 »
Hi Guys,

I'm trying to get variables from a Type using reflection. I can get Fields, but EnumGlobals() doesn't return any data.

Code: [Select]
Type TPlayer
Field x:Int = 300
Field y:Int = 100
Field name:String = "Scaremonger"
Const SIZE:Int=99
Global Amount:Float = 0.782
End Type

Local player:TPlayer = New TPlayer()
Local this:TTypeId = New TTypeId.ForObject( player )
Local dbg:TList

dbg = this.EnumGlobals()
Print( "Global Count:" + dbg.count() )
'Local first:Object = dbg.first()
For Local fld:TField = EachIn this.EnumGlobals()
Local typ:TTypeId = fld.typeid ' Field type
Local value:String = fld.getString( player )
Print( "  Global "+ fld.name + ":" + typ.name + " = " + value )
Next

dbg = this.EnumFields()
Print( "Field Count:" + dbg.count() )
'Local first:Object = dbg.first()
For Local fld:TField = EachIn this.EnumFields()
Local typ:TTypeId = fld.typeid ' Field type
Local value:String = fld.getString( player )
Print( "  Field "+ fld.name + ":" + typ.name + " = " + value )
Next

Am I missing something here?

Si...

Offline Derron

  • Hero Member
  • *****
  • Posts: 3667
Re: Extracting Globals from Type using Reflection
« Reply #1 on: January 31, 2021, 12:20:36 »
Open up brl.mod/reflection.mod/reflection.bmx

go to line 2512, replace
Code: BlitzMax
  1.                 For Local t:TField=EachIn _globalsList '.Values()
  2.  
with
Code: BlitzMax
  1.                 For Local t:TGlobal=EachIn _globalsList '.Values()
  2.  


Raised an issue for it (and a Pull Request to incorporate the change with "one click") ... but ... for now it is uncertain when this will be officially fixed/merged.


bye
Ron
« Last Edit: January 31, 2021, 12:24:33 by Derron »

Offline Scaremonger

  • Full Member
  • ***
  • Posts: 233
    • ITSpeedway - Ramblings of a geek!
Re: Extracting Globals from Type using Reflection
« Reply #2 on: February 01, 2021, 17:16:37 »
Thanks @Derron. That explains why it didn't work! Lol

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal