【发布时间】:2010-12-03 20:17:02
【问题描述】:
我有一些代码在索引 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
【问题讨论】:
-
您需要提供您的代码,这样我们才能看到实际发生了什么。
-
是的,请务必显示重现问题的最小代码,仅从口头描述中无法调试它。
标签: .net exception dictionary key