时报告无效密钥我有一些代码在索引0处将一个Func<short>
添加到Dictionary<byte, Func<short>>
。稍后,包含该字典的类中的某些代码尝试提取此Func(通过TryGetValue)和执行它,如果不起作用则抛出异常。即使被访问的索引是有效的,它也会抛出表示函数提取失败的异常。为什么是这样?在开关的情况下沙漠在你的代码(可以根据需要提供代码)字典<>在不是
//processor construction and setup
VirtualProcessor processor = new VirtualProcessor();
...
processor.InputChannels.Add(0,() => { return 2; });//the func i'm trying to access
//end processor construction and setup
//build program
Dictionary<short, Command> commands = new Dictionary<short, Command>();
commands.Add(0, CommandFactory.CreateInputCommand(0, 0));//this, in a roundabout way, attempts to call the func
commands.Add(1, CommandFactory.CreateLoadCommand(1, 200));
commands.Add(2, CommandFactory.CreateAddCommand(0, 1, 2));
commands.Add(3, CommandFactory.CreateHaltCommand());
//end build program
//execution
processor.CurrentProgram = commands;
processor.ExecuteTillHalt();
//end execution
Console.ReadLine();
某处,在另一个类...
Func<short> inChannel;
InputChannels.TryGetValue(command.Data2, out inChannel);//the attempt to access the func, this is the second value supplied to CreateInputCommand above
if (inChannel != null)
Registers[command.Data1] = inChannel();//should be here
else
throw new InvalidInputChannelException("Invalid input channel " + command.Data2); //throws this
您需要提供你的代码,所以我们可以看到什么是真正* *发生。 –
是的,请确实显示重现问题的最小代码,不可能从这个口头描述中进行调试。 –