Reflection: Arguments

Monkey Forums/Monkey Programming/Reflection: Arguments

Shinkiro1(Posted 2012) [#1]
When getting the function info like this:
Local fInfo:FunctionInfo = GetFunction ("Add", [])   ' <--- ???
fInfo.Invoke ( [BoxInt(10), BoxInt(5)] )

Function Add:Void (a:Int, b:Int)
  Print (a + b)
End


On line 1, what do I have to put in the array?
The Docs say a ClassInfo array but I can't make any sense of that.


Paul - Taiphoz(Posted 2012) [#2]
Wait... sorry to hijack this but if I am understanding this correctly.

With reflection you can get a function into a variable and then call it with invoke, meaning that in your case finfo which you made reflect Add you could have made that reflect any function, and indeed any function during any part of your code.

WOW!. I think I kinda get what that's all about now, thanks m8 your little code snippet helped me a lot hope you get a solution to your problem.


marksibly(Posted 2012) [#3]
Hi,

You need to put the param types for add inside the array.

In this case, use:

GetFunction( "Add",[IntClass(),IntClass()] )

The IntClass function returns the class of the objects used to box ints - ie: the class of the objects you actually pass to invoke.

You have to pass parameter types with all GetMethod/GetFunction calls because method/function overloading means there may be more than one method/function with the same name.

Fixed demo:
Import reflection

Function Main()
	Local fInfo:FunctionInfo = GetFunction ( "Add", [IntClass(),IntClass()] )

	fInfo.Invoke ( [BoxInt(10), BoxInt(5)] )
End

Function Add:Void (a:Int, b:Int)
  Print (a + b)
End



Shinkiro1(Posted 2012) [#4]
Thanks, all working now.
I didn't realize that IntClass() is a function ^^