【发布时间】:2016-10-07 18:05:52
【问题描述】:
我收到了似乎来自以下代码的错误报告:
public class AnimationChannelCollection : ReadOnlyCollection<BoneKeyFrameCollection>
{
private Dictionary<string, BoneKeyFrameCollection> dict =
new Dictionary<string, BoneKeyFrameCollection>();
private ReadOnlyCollection<string> affectedBones;
// This immutable data structure should not be created by the library user
internal AnimationChannelCollection(IList<BoneKeyFrameCollection> channels)
: base(channels)
{
// Find the affected bones
List<string> affected = new List<string>();
foreach (BoneKeyFrameCollection frames in channels)
{
dict.Add(frames.BoneName, frames);
affected.Add(frames.BoneName);
}
affectedBones = new ReadOnlyCollection<string>(affected);
}
public BoneKeyFrameCollection this[string boneName]
{
get { return dict[boneName]; }
}
}
这是读取字典的调用代码:
public override Matrix GetCurrentBoneTransform(BonePose pose)
{
BoneKeyFrameCollection channel = base.AnimationInfo.AnimationChannels[pose.Name];
}
这是创建字典的代码,在启动时发生:
// Reads in processed animation info written in the pipeline
internal sealed class AnimationReader : ContentTypeReader<AnimationInfoCollection>
{
/// <summary>
/// Reads in an XNB stream and converts it to a ModelInfo object
/// </summary>
/// <param name="input">The stream from which the data will be read</param>
/// <param name="existingInstance">Not used</param>
/// <returns>The unserialized ModelAnimationCollection object</returns>
protected override AnimationInfoCollection Read(ContentReader input, AnimationInfoCollection existingInstance)
{
AnimationInfoCollection dict = new AnimationInfoCollection();
int numAnimations = input.ReadInt32();
/* abbreviated */
AnimationInfo anim = new AnimationInfo(animationName, new AnimationChannelCollection(animList));
}
}
错误是:
索引超出了数组的范围。
行:0
在 System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
在 System.Collections.Generic.Dictionary`2.get_Item(TKey key)
在 Xclna.Xna.Animation.InterpolationController.GetCurrentBoneTransform(BonePose 姿势)
我本来希望 KeyNotFoundException 带有错误的键,但我却得到“索引超出了数组的范围”。 我不明白我怎么能从上面的代码中得到那个异常?
顺便说一下,代码是单线程的。
【问题讨论】:
-
代码无法编译。当然你的意思是
return dict[boneName]; -
getter 的回报在哪里?
-
此外,一旦添加正确的返回,我正确收到
KeyNotFoundException。您需要更新您的代码以正确重现该问题,因为它目前没有。
标签: c# dictionary