dimanche 25 août 2019

Is there a way to declare method that takes any lambda as parameter?

I'm trying to write a method in C# that will accept lambda of any type, but I can't figure out how to do it.

I'm developing a class that will process text commands. I want to implement it in such a way that arguments will be automatically determined from lambda expression that handles particular command.

Here is an example of what API I would like to get:

commandProcessor.AddCommand("setpos", (int x, int y)=>{
    //do stuff
});
commandProcessor.AddCommand("changename",(string newName)=>{
    //do completely different stuff
});

And my command processor will look like this:

Dictionary<string, MagicType> mCommands;

public void AddCommand(string commandName, MagicType commandHandler){
    mCommands[commandName] = commandHandler;
}

Is there a MagicType that I can use or maybe I should use a completely different approach?





Aucun commentaire:

Enregistrer un commentaire