Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to save a function-call for in memory for later invoking

Is there any way in C# to save a function call for later invoking? For example, I want to be able to say:

public class MyFunctionCaller
{
    public static void SaveFunctionCallForLater(/* Some parameters*/)
    {
    // Save the Function Call in a variable or a list to be invoked after a set amount of time
   }
}

public class MyProgram
{
    public static void Main()
    {
        // Save a call to 'SpecialFunction' along with its parameters,
        // and maybe the object that's calling it (not applicable in
       // this example context, but important to my question)
        MyFunctionCaller.SaveFunctionCallForLater(/* Stuff */);
    }

    public void SpecialFunction(/* Stuff */)
    {
        // Does some cool stuff
    }
}

Let me give you some context: I'm creating a game in XNA and I want to create a DelayedFunctionCaller class, that any object can reference to hand complete function calls to, along with an amount of time to wait before it calls the function. I handle all of the time-waiting and triggering myself so that's not where I'm lost, I'm just not sure how or the correct way to package up the function to be passed to the DelayedFunctionCaller.

Here's the kicker: The DelayedFunctionCaller has to be able to run any function, no matter what object is sending it a function, what the function returns, or what parameters it takes**. I have over 100 classes in my game already, and the goal is to create an object that can save any function call from any one of them for later calling.

So from my own research, I've found the Type class, and I know that I can save the type of the object calling, the function name as a string (which is an acceptable sacrifice), then use GetMethod() to save the method in a MemberInfo, then use MemberInfo's Invoke() to call the function, even on a saved object, and with the parameters (as an array of Objects).

But this all seems very... hack-ish. I tried to look this up once and thought that Generic Delegates was the way I wanted to go, but I got so lost in there that I gave up. Am I on the right track? Is this the only way to do this? What about Generic Delegates, or was I just out of my mind when I thought it would solve my problem?

Any and all help is appreciated. Thank you!

like image 703
KeithA45 Avatar asked Jun 03 '12 03:06

KeithA45


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

C dalam Latin berapa?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).

Bahasa C dibuat pertama kali oleh siapa dan tahun berapa?

Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.


1 Answers

You could save it as an Action, using a lambda expression:

Action functionToCallLater = () => SpecialFunction(/* params */);
// later
functionToCallLater(); // calls SpecialFunction with its parameters

If the function returns something and you need its value, simply use the Func<T> type instead.

int MyMethod(int param) { return param + 1; }
(...)
Func<int> functionToCallLater = () => MyMethod(3);
(...)
int result = functionToCallLater(); // result == 4

Keep in mind that any variable you use within the lambda is captured itself, not its value; if its value changes, that'll be the value used when the function is called. See Eric Lippert's blog for a deeper explanation.

like image 50
Asik Avatar answered Oct 21 '22 23:10

Asik