【问题标题】:Loading projects using Microsoft.VisualStudio.LanguageServices throws System.ArgumentNullException for Visual Studio 2017使用 Microsoft.VisualStudio.LanguageServices 加载项目会引发 Visual Studio 2017 的 System.ArgumentNullException
【发布时间】:2020-12-29 11:16:23
【问题描述】:

我正在尝试使用 Microsoft.VisualStuio.LanguageServices 枚举解决方案的所有项目。 这在 VisualStudio 2019 上运行良好,但在 VisualStudio 2017 上却失败了。 我的代码如下-

var workspace= ComponentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>(); 
var projects = workspace.CurrentSolution.Projects;        

我在第二行遇到异常 -

System.ArgumentNullException: '值不能为空。参数名称:source'

它显示了调试中的项目列表。 我正在使用 2.10.0.0 版本的 Microsoft.VisualStudio.LanguageServices.dll,它与 VS 模块中加载的 dll 相同。 是组装问题吗?我该如何解决这个问题?

【问题讨论】:

  • 根据我的测试,我发现我不能使用你提供的代码。你能告诉我你添加了什么引用或你添加了 nuget-package 吗?
  • 我刚刚发现了这个问题。 Microsoft.VisualStudio.LanguageService.dll 2.10.0 版与我所指的 Microsoft.CodeAnalysis.dll 3.7.0.0 版不兼容。 Visual Studio 2017 使用 Microsoft.CodeAnalysis.dll 版本 2.10.0.0,但它不会在 Module 中加载。

标签: c# visual-studio-2017 roslyn vsix roslyn-code-analysis


【解决方案1】:

这个问题是由于引用的 dll 和 VS2017 的 dll 之间的 Microsoft.CodeAnalysis 版本不匹配造成的。

在模块窗口中可以清楚地看到,VS 使用的是 Microsoft.VisualStudio.LanguageServices 2.10.0.0,因此我保留了相同版本的参考 dll。

在此声明中- var workspace= componentModel.GetService&lt;Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace&gt;(); 工作区的类型为 Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace。

但是当我们这样做时 - var projects = workspace.CurrentSolution.Projects;

变量“项目”的类型是 IEnumerable

我想,这就是调试值在快速监视中可用但执行失败的原因。

解决方案 - 引用 VS 引用的相同版本的 dll。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多