Is it possible to push some args myself and then call C# function???Masters come in and help me

say if there were a C# function like

void printIntAndString(int value, string aString);

xxx.pushRefArg(myStr);(my imaginary function)
xxx.pushValueArg(5);(my imaginary function)
yyy.call(printIntAndString);(also my imaginary function)

anyone could tell me does C# or mono support calling function in this way?
I want to have some knowledge of the very low level code…
if it does, what are xxx and yyy.
and is it supported in mobile platform…

Not completely understanding your syntax, but I’ll take a guess.
You may be looking to declare a delegate

delegate void printIntAndString(int value, string aString);

This way, you can write a function that’s implemented like

void call(printIntAndString printFunction)
{
    if (printFunction != null)
    {
        printFunction(5, "hello");
    }
}

As something like this:

void MyFunction(int value, string str)
{
    // do something
}

myObject.call(MyFunction);

But again, it’s kind of difficult to tell exactly what you are trying to do.

No idea, I’ve never needed to get that low level before. Here are a few suggestions. They may be wrong.

Try looking at unsafe code blocks. I’m not sure even this will let you go low level enough.

You can also use native .dlls compiled from C++. The performance advantage will new to be balanced against the cost of calling into the code.

But as general advice I’d say let the compiler do this stuff. What are you actually using it for?

Actually if you aren’t after this for performance and instead simply want to manipulate methods on a very low level the you can do this sort of thing with reflection.

It will be slower then calling a method directly, but it gives you incredible flexibility to invoke a method however you choose.

To me, the big question is why do you want to call methods this way?

1 Like

the question is: it not C#, the code is a string interpreted by an interpreter
I want to know how the interpreter works…like lua interpreter
it finds a parameter, push it, and push and push, and finally call that function which is binded with a C# function.
reflection is too slow for a call

I want to know how the interpreter function works…like lua interpreter

function callThis(arg1, arg2)
    printArg(arg1, arg2)    
end

in this imaginary interpreter, printArg is actually connected to a C# function.I can index it by a Dictionary<string, Action> or anything like. in order to make a quick call, I think pushing arg1 and arg2 and then invoking C#printArg is faster than delegate or whichever way …

What you’re looking for is this:

Do you mean the underlying IL?

ah…I dont know what underlying IL is, but I guess lua interpreter calls function in this way…It’s all I guess…

Well, aside from basic reflection you can look into Reflection.Emit, Expression Trees, or CodeDOM. These will let you dynamically compile code at runtime, but I don’t know if they are available in Unity’s Mono.