【问题标题】:Code Analysis tool errors with "The given key was not present in the dictionary."“字典中不存在给定键”的代码分析工具错误。
【发布时间】:2010-07-24 23:15:03
【问题描述】:

运行代码分析时,我在构建我的 .NET 项目时收到以下无用的错误。它既发生在 Visual Studio 中,也发生在使用 MSBuild 从命令行构建。

  * 1 total analysis engine exceptions.
MSBUILD : error : CA0001 : The given key was not present in the dictionary.

关于什么是坏的有什么想法吗?


编辑:

找到导致它的代码。我的代码中有一个方法。

Public Function Generate(ByVal input As XDocument) As XDocument
   ' My code
End Function

如果我添加以下行作为第一行代码,错误就会开始发生,如果我删除它,错误就会停止。

Contract.Requires(Of ArgumentNullException)(partCover IsNot Nothing, "input")

这并没有什么意义,因为我在整个项目中都在使用代码合同。此方法与其他方法的唯一不同之处在于它包含大约 200 行 XML 文字。该方法将输入文档转换为另一种类似于 XSLT 的 XML 格式。我的猜测是它与此有关。

【问题讨论】:

  • 在 VS2017 中也可以看到,其代码在 VS2013 中没有显示。在我们的例子中,我们通过意外应用了错误的规则集触发了这一点。

标签: .net visual-studio-2010 code-analysis fxcop


【解决方案1】:

这是工具的内部错误。尽管听起来您找到了临时解决方法,但您对此无能为力。当您继续处理源代码时,它可能会自行解决。尽管这需要一个半满的玻璃杯。

您可以在 connect.microsoft.com 上报告该错误,他们需要您的一小部分代码样本,以便为他们重现该错误。

【讨论】:

    【解决方案2】:

    我也遇到过类似的问题:

    <Exceptions>
      <Exception Keyword="CA0001" Kind="Engine">
       <Type>System.Collections.Generic.KeyNotFoundException</Type>
       <ExceptionMessage>The given key was not present in the dictionary.</ExceptionMessage>
       <StackTrace>   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodQueue.ReportDeclaringMethodFound(FunctionSymbol anonymousMethod)
       at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodPhase.ExamineInstructionForAnonymousMethodDeclaration(Instruction instruction)
       at Microsoft.FxCop.Engines.Phoenix.PreScanPass.AnonymousMethodPhase.Execute(Unit unit)
       at Phx.Phases.PhaseList.DoPhaseList(Unit unit)
       at Microsoft.FxCop.Engines.Phoenix.PreScanPass.Execute(ModuleUnit moduleUnit)
       at Phx.Passes.PassList.DoPassList(ModuleUnit moduleUnit)
       at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.AnalyzeInternal()
       at Microsoft.FxCop.Engines.Phoenix.PhoenixAnalysisEngine.Analyze()
       at Microsoft.FxCop.Common.EngineManager.Analyze(Project project, Boolean verboseOutput)    </StackTrace>
    </Exception>
    

    我做了一些调查,发现只有从 Visual Studio 2010 运行 FxCop 时才会出现该问题。由于它在 VS2012 中运行良好,很可能 FxCop 中的错误已得到修复。

    在逐个方法注释掉我的更改后,我认为这是由同一类中的 2 个匿名方法(工作正常,只有一个)引起的。一旦我将它们转换为“命名”方法,错误就消失了。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      相关资源
      最近更新 更多