我有一个字典,其中键是一个字符串,值是一个接受两个参数(字符串和字节数组)的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参数?
发布于 2020-03-02 16:06:14
您应该在字典中使用与要分配的类型相同的类型:
private Dictionary<string, Action<string, byte[]>> handlers
= new Dictionary<string, Action<string, byte[]>>();然后,您的KeyValuePair cb将具有与您的handlers相同的TValue
public void Bind(string key, Action<string, byte[]> cb)
{
handlers[key] = cb;
}https://stackoverflow.com/questions/60484531
复制相似问题