private Dictionary<Type, Bag<Event>> events = new Dictionary<Type, Bag<Event>>();
internal Bag<T> GetEventList<T>() where T:class
{
Type type = typeof(T);
Bag<Event> b;
if (events.TryGetValue(type, out b))
{
return b as Bag<T>;
}
else {
b = new Bag<T>() as Bag<Event>;
events[type] = b;
return b as Bag<T>;
}
}
internal void AddEvent<T>(T ev) where T:class,Event
{
Type type = typeof(T);
Bag<Event> b;
if (events.TryGetValue(type, out b))
{
b.Add(ev); // <= NullReferenceException, b is null
}
else {
b = new Bag<T>() as Bag<Event>;
events.Add(type, b);
b.Add(ev);
}
}
我总是在 AddEvent 中得到 NullReferenceException。事件字典仅在这两个函数中使用,我不知道为什么值为空......我没有在任何地方插入空值!
我在这里疯了...