reflectiontest.monkey2 1018 B

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