Dictionary of Actions - Include Action parameters

Viewed 100

I have a dictionary where the key is a string, and the value is an Action that takes two parameters (a string and a byte array)

private Dictionary<string, Action> handlers = new Dictionary<string, Action>();

Then a function to add a value to the dictionary

public void Bind(string key, Action<string, byte[]> cb)
{
    handlers[key] = cb;
}

However the error is "Cannot convert System.Action to System.Action"

How can I change the definition of the dictionary to include the Action parameters?

1 Answers

You should use the same type in your dictionary with what you want to assign:

private Dictionary<string, Action<string, byte[]>> handlers 
    = new Dictionary<string, Action<string, byte[]>>();

Then your KeyValuePair cb will have the same TValue of your handlers:

public void Bind(string key, Action<string, byte[]> cb)
{
    handlers[key] = cb;
}
Related