动作字典-包括动作参数



我有一个字典,其中键是一个字符串,值是一个Action,它接受两个参数(一个字符串和一个字节数组(

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

然后是一个向字典添加值的函数

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

但是错误是"无法将System.Action转换为System.Action">

如何更改字典的定义以包含Action参数?

您应该在字典中使用与您想要分配的类型相同的类型:

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

那么您的KeyValuePaircb将具有与handlers:相同的TValue

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

最新更新