c# - 使用 IDebugEventCallback2 的 VS2019 扩展

标签 c# visual-studio visual-studio-extensions visual-studio-2019

我使用 VS2019 16.1.6 创建了一个新的 VS 扩展。 我添加了这个 using 语句

using Microsoft.VisualStudio.Debugger.Interop;

并将接口(interface) IDebugEventCallback2 添加到我的类中

public sealed class VSIXProject1Package : AsyncPackage, IDebugEventCallback2

不是我收到错误:

error CS0433: The type 'IDebugEventCallback2' exists in both 'Microsoft.VisualStudio.Debugger.Interop, Version=8.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'Microsoft.VisualStudio.Debugger.InteropA, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

如何消除这个错误?或者除了使用 IDebugEventCallback2 之外还有其他方法来响应调试器事件吗?

编辑: 向 Microsoft 报告的问题: https://developercommunity.visualstudio.com/content/problem/651199/vs2019-extension-using-idebugeventcallback2.html

最佳答案

VS2019使用PackageReference格式来管理 VSIX 项目的 nuget 包。

默认情况下,它将引用 Microsoft.VisualStudio.SDKMicrosoft.VSSDK.BuildTools 包。此外,因为 Microsoft.VisualStudio.SDK > 包依赖于许多其他包,该项目也将引用这些包。

看看这个简单的结构:

Microsoft.VisualStudio.SDK
   ......(other dependencies)
   --Microsoft.VisualStudio.Debugger.Interop
      --Microsoft.VisualStudio.OLE.Interop
   --Microsoft.VisualStudio.Debugger.Interop.10.0
      --Microsoft.VisualStudio.Debugger.InteropA
   ......(11.0,12.0,14.0,15.0)
   --Microsoft.VisualStudio.Debugger.Interop.16.0
      --Microsoft.VisualStudio.Debugger.InteropA

很明显,此问题是由于 VSIX 项目添加了对 Microsoft.VisualStudio.Debugger.InteropMicrosoft.VisualStudio.Debugger.InteropA 的引用造成的。 这两个程序集具有相同的命名空间 Microsoft.VisualStudio.Debugger.Interop,并且都具有 IDebugEventCallback2 接口(interface)。我认为这就是导致此问题的原因。

解决方法:

正常我们可以使用extern alias对于这种情况。但它还不支持 PackageReference 格式。幸运的是我从gertjvr找到了一个很好的提示。所以我们需要的是:

卸载项目=>编辑xxx.csproj=>将以下内容添加到项目文件中:

  <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Microsoft.VisualStudio.Debugger.Interop'">
        <Aliases>signed</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

如果要使用此程序集中的接口(interface),可以将 ReferencePath 更改为 Microsoft.VisualStudio.Debugger.InteropA。这取决于您的需要。

关于c# - 使用 IDebugEventCallback2 的 VS2019 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57074957/

相关文章:

visual-studio - 最小的工作 IVsTextViewCreationListener 实现是什么?

c# - 如何获取 liblz4.dll

c# - COM 互操作对象在一个项目中抛出 InvalidCastException,但在其他项目中没有

用于 C++ 的 C# 包装器,但仅编译为静态库

c++ - Visual Studio 2010 上的 VC++ : Release builds using debug heap

visual-studio-2017 - Visual Studio 中的消息 "This extension is scheduled for install"

c# - 在 C# 中获取和设置

c# - 文本框 - 绑定(bind)的属性值不会立即更新

c# - 登录应用程序的问题

visual-studio-2013 - 如何在 visual studio 中安装特定的扩展版本