c# - 奇怪的 "name does not exist in current context"错误

标签 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

最佳答案

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

我们在 Release模式下生成 .pdb 文件,这就是调试仍然可以正常工作的原因……直到那个变量神秘地消失了。

关于c# - 奇怪的 "name does not exist in current context"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31850193/

相关文章:

c# - .Net 表单 POST

c# - 无法从程序集 'Microsoft.TeamFoundation.TFStringComparer' 加载类型 'Microsoft.TeamFoundation.Common'

.net - C# 垃圾收集器交叉引用

.net - 为什么此 DataAdapter 不将行插入数据库?

php - PHP解析/语法错误;以及如何解决它们

c++ - 调试:我的符号没有加载到 gdb 中?

C# 三元表达式类型转换

c# - 未将对象引用设置为 ComboBox 中对象的实例

jquery - IE中的CSS问题。 Jquery 图像/swf 交换

c# - 如何根据 numericupdown 值实时更改定时器间隔?