Reflection Invoke question

Monkey Forums/Monkey Programming/Reflection Invoke question

wiebow(Posted 2012) [#1]
Has anyone solved this in reflection?

When I want to use MethodInfo.Invoke() I need to pass an array for parameters. What if the method I want to invoke does not require parameters? I cannot pass Null as the parameter as that throws a compile error.


update: adding a new array, say using Local params:Int[1] , will result in the error: Unable to find overload for Invoke(object,Int[])
I know where that is coming from. But how to fix it?


JIM(Posted 2012) [#2]
Did you try with "Void" or a null array ( "[]" ) ?


wiebow(Posted 2012) [#3]
Null array I did try, Void: no. I will give that a shot.

edit: Bothh of these don't work. This is a real problem atm. Boxing a value (as the documentation suggests) is also not working. I am then getting "Cannot find overload for Invoke(object, object)"


marksibly(Posted 2012) [#4]
Hi,

Use an empty array, eg:

Import reflection

Class C
	Method Update()
		Print "C.Update!"
	End
End

Function Main()
	Local c:=GetClass( "C" )
	Local i:=c.NewInstance()
	Local m:=c.GetMethod( "Update",[] )
	m.Invoke( i,[] )
End



wiebow(Posted 2012) [#5]
Ah, the trick is using an emtpy array when using GetMethod(). Thanks!