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?
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?
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 .
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;
Source: https://ru.stackoverflow.com/questions/646534/
All Articles