public Dictionary<string, Func<List<string>, object>> operations = new Dictionary<string, Func<List<string>, object>>(); operations.Add("myComand", myMethod); 

Explain, please, how to implement such a dictionary?

Closed due to the fact that the essence of the issue is incomprehensible to the participants of Kromster , Pavel Mayorov , αλεχολυτ , AK , Denis Bubnov 3 Apr '17 at 8:52 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You yourself wrote in the question of its implementation. What are the difficulties? - Exodium
  • @Exodium can't figure out how to practically apply - performance

2 answers 2

As an option:

 public class Commands<T> { public readonly Dictionary<string, T> CommandDictionary = new Dictionary<string, T>(); public void AddCommandToDictionary(string command, T action) { CommandDictionary.Add(command, action); } } 

Create an object:

 Commands<Func<bool>> _commands = new Commands<Func<bool>> (); 

And add:

 _commands.AddCommandToDictionary("input_command", () => { SomeUsefulFunction(); return true; }); 

And the use itself:

 Func<bool> cmd; if (_commands.CommandDictionary.TryGetValue(input, out cmd)) { cmd(); } 

    Func<List<string>, object> is a delegate that can be initialized with a method with the prototype specified in the Func delegate.

    In your case, create methods with a prototype:

     public object MyMethod(List<string> input) 

    Next, assign the names of the methods to the delegate and put them in the dictionary using the Dictionary.Add () method;