You are not logged in.

#1 2006-07-06 16:09:59

cmp
Member
Registered: 2005-01-03
Posts: 350

MONO using Reflection to invoke a method

Hi.

I want to call a method indirectly by using MethodInfo.Invoke. For methods without a parameter it works allready, but when trying to pass parameters it throws an exception.

This is my test object:

public class Test{    
    public void method(int a, float b){
        Console.WriteLine("{0} {1}", a, b);
    }
}

And this way I try to call them

Test my_test = new Test();
object[] par = new object[2] {2, 2.0};
MethodInfo meth = my_test.GetType().GetMethod("method");
meth.Invoke(my_test, BindingFlags.Default, null, par, null);

Mono says that I have to pass an object[] array with all paremters inside in the exact same order and the same type as the method expect me to. But I guess that I do, at least 2 and 2.0 should be int and float.

I hope that someone of you got an idea how to deal with this.

Offline

#2 2006-07-06 17:16:38

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: MONO using Reflection to invoke a method

object[] par = new object[2] {2, 2.0f};

you also don't need the full method.
meth.Invoke(my_test, par);
will work just fine.

Offline

#3 2006-07-06 17:22:43

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: MONO using Reflection to invoke a method

thanks. work's like a charm.

Offline

Board footer

Powered by FluxBB