2
当我运行这段代码我得到一个不寻常的空引用。不寻常的NullReferenceException在mscorlib.dll
刚刚放晴,我明白了一个空引用是什么,没有了在此方法中使用的值为null在运行时。空引用似乎在mscorlib中的某处,我一直无法找到任何人报告类似的问题到目前为止。
特征与在它10个左右的项的枚举。
private Dictionary<Feature, bool> dict = new Dictionary<Feature, bool>();
public bool AddFeature(Feature val)
{
if (!dict.ContainsKey(val))
{
dict.Add(val, false);
return true;
}
else
return false;
}
特征
[Flags]
public enum Feature
{
[Description("Other")]
Other = 0x00000000,
[Description("Analysis")]
Analysis = 0x00000001,
[Description("Campaign")]
Campaign = 0x00000002,
[Description("Trends")]
Trends = 0x00000004,
[Description("Portal")]
Portal = 0x00000008,
[Description("Phone")]
Phone = 0x00000010,
[Description("Rents")]
Rents = 0x00000020,
[Description("Repairs")]
Repairs = 0x00000040,
[Description("Maintenance")]
Maintenance = 0x00000080,
[Description("Management")]
Management = 0x00000100,
[Description("Services")]
Services = 0x00000200,
[Description("All")]
All = 0x7FFFFFFF
}
错误是:
类型 'System.NullReferenceException' 的一个例外发生在mscorlib.dll但在用户代码中没有处理
附加信息:未将对象引用设置为对象的实例。
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Test.Model.EnabledFeatures.AddFeature(Feature val) in C:\Sandbox\Test\Shared\Model\EnabledFeatures.cs:line 26
我已经通过我的代码,并加强对
dict.Add(val, false);
行发生错误。当我在那一点上休息时,dict
和val
都不为空,并且都有它们的期望值。
此代码用来工作,后来才知道是远离它的几个星期,回来时发现它失败。我想知道如果一些Windows或Visual Studio的更新可以打破这个?
的可能的复制[什么是一个NullReferenceException,如何解决呢?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix - 它) – john
a)它看起来像你想要一个HashSet。 b)val为null或val的值为null,并且您有一个正在尝试使用该空值的自定义比较器。 – john
@john:情况并非如此。 OP看起来并不像他对这个例外是未知的。他无法想到的是这发生的地方。 –