reflectiontest.monkey2 1002 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. Namespace test
  2. 'Must import this to use reflection!
  3. '
  4. #Import "<reflection>"
  5. 'A test global...
  6. '
  7. Global G:Int
  8. 'A test class...
  9. '
  10. Class C
  11. Method Update( msg:String )
  12. Print "C.Update! msg="+msg
  13. End
  14. End
  15. Function Main()
  16. 'Some Typeof examples...
  17. '
  18. Local x:=10
  19. Print "Typeof( x )="+Typeof( x )
  20. Print "Typeof( Main )="+Typeof( Main )
  21. Print "Typeof<Int Ptr[]>="+Typeof<Int Ptr[]>
  22. Print "Typeof<C>="+Typeof<C>
  23. 'Show all decls
  24. '
  25. For Local type:=Eachin TypeInfo.GetTypes()
  26. Print type
  27. For Local decl:=Eachin type.GetDecls()
  28. Print "~t"+decl
  29. Next
  30. Next
  31. 'Dynamically create a 'C' and invoke Update:
  32. '
  33. Local ctype:=TypeInfo.GetType( "test.C" )
  34. Local ctor:=ctype.GetDecl( "New" )
  35. Local cvar:=ctor.Invoke( Null,Null )
  36. ctype.GetDecl( "Update" ).Invoke( cvar,New Variant[]( "Hello world!" ) )
  37. Cast<C>( cvar ).Update( "Hello again!" )
  38. 'Modify the 'G' global:
  39. '
  40. Local g:=TypeInfo.GetType( "test" ).GetDecl( "G" )
  41. g.Set( Null,1234 )
  42. Print Int( g.Get( Null ) )
  43. Print G
  44. End