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

时间:2015-08-06 08:08:01

标签: c# .net debugging

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

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

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

Debugging screenshot

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

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

更多信息:

  • 使用Resharper的单元测试运行器从单元测试(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<class Ihc.Integration.ParserBuilder.Hl7.Core.SchemaConfigurations.Domain.SegmentConfiguration>::.ctor()
IL_0005: stloc.0
IL_0006: newobj instance void class [mscorlib]System.Collections.Generic.List`1<class Ihc.Integration.ParserBuilder.Hl7.Core.SchemaConfigurations.Domain.SegmentConfiguration>::.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

1 个答案:

答案 0 :(得分:0)

有人搞砸了.sln文件中的配置。即使“有效”配置为release,所有项目也都设置为debug配置。

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