奇怪的“当前上下文中不存在名称”错误

我正在调试Visual Studio 2015中的以下代码片段:

Stream xmlFileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceFilePath); if (xmlFileStream != null) { var reader = XmlReader.Create(xmlFileStream); XDocument schemaDefinition = XDocument.Load(reader); // NullReference ... 

最后一行抛出一个NullReferenceException ,所以我开始调试,我有一个最奇怪的情况:

调试截图

因此,抛出NullReferenceException的行, reader的声明/初始化下面的行,我得到的reader不存在。

这怎么可能? 我甚至重新启动了我的电脑,我已经清理了几十次重建项目。

更多信息:

  • 代码使用Resharper的unit testing运行器从unit testing(NUnit)运行。
  • 目标框架是.NET 4(测试和代码程序集)
  • xmlFileStream在那一点看起来很好

进一步向上调用堆栈似乎有一些与线程相关的东西(我对项目还不熟悉,尚未确定它的使用原因):

 lock (Threadlock) { if (!SchemaConfigurations.Keys.Contains(dialect)) { SchemaConfigurations.Add(dialect, new SchemaConfiguration { /*DOING THIS*/ MessageStructureConfigurations = ReadMessageStructureConfiguration(dialect), SegmentConfigurations = ReadSegmentConfiguration(dialect), DataTypeConfigurations = ReadDataTypeConfiguration(dialect), MessageTypeLocation = _messageTypeLocation, TriggerEventLocation = _triggerEventLocation }); } } 

这是IL(使用ILSpy):

 IL_0000: newobj instance void class [mscorlib]System.Collections.Generic.List`1::.ctor() IL_0005: stloc.0 IL_0006: newobj instance void class [mscorlib]System.Collections.Generic.List`1::.ctor() IL_000b: stloc.1 IL_000c: ldstr "{0}.Hl7.Core.SchemaConfigurations.Schemas.{1}_Segments.xml" IL_0011: call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::GetExecutingAssembly() IL_0016: callvirt instance class [mscorlib]System.Reflection.AssemblyName [mscorlib]System.Reflection.Assembly::GetName() IL_001b: callvirt instance string [mscorlib]System.Reflection.AssemblyName::get_Name() IL_0020: ldarg.0 IL_0021: box Ihc.Integration.ParserBuilder.Hl7.Interfaces.SupplierType IL_0026: call string [mscorlib]System.String::Format(string, object, object) IL_002b: stloc.2 IL_002c: call class [mscorlib]System.Reflection.Assembly [mscorlib]System.Reflection.Assembly::GetExecutingAssembly() IL_0031: ldloc.2 IL_0032: callvirt instance class [mscorlib]System.IO.Stream [mscorlib]System.Reflection.Assembly::GetManifestResourceStream(string) IL_0037: stloc.3 IL_0038: ldloc.3 IL_0039: brfalse IL_0339 IL_003e: ldloc.3 IL_003f: call class [System.Xml]System.Xml.XmlReader [System.Xml]System.Xml.XmlReader::Create(class [mscorlib]System.IO.Stream) IL_0044: call class [System.Xml.Linq]System.Xml.Linq.XDocument [System.Xml.Linq]System.Xml.Linq.XDocument::Load(class [System.Xml]System.Xml.XmlReader) IL_0049: stloc.s 4 IL_004b: ldloc.s 4 

有人搞砸了.sln文件中的配置。 所有项目都设置为在release配置中构建,即使“活动”配置是debug

我们在发布模式下生成.pdb文件,这就是为什么调试仍然正常工作……直到那个变量神秘地消失。