找不到互操作类型“Microsoft.Internal.VisualStudio.Shell.Interop.SVsColorThemeService”

Cannot find the interop type'Microsoft.Internal.VisualStudio.Shell.Interop.SVsColorThemeService'

我正在使用 Visual Studio 2013 更新 4 社区版构建 Visual Studio package/extension。我正在使用可扩展性项目组下 Visual Studio SDK 附带的标准项目模板。

我需要的是 SVsColorThemeService 但是当我在 Initialize 方法中添加以下行时

 var svc = GetGlobalService(typeof(SVsColorThemeService)) as IVsColorThemeService;

我遇到两个构建错误:

Cannot find the interop type that matches the embedded interop type 'Microsoft.Internal.VisualStudio.Shell.Interop.SVsColorThemeService'. Are you missing an assembly reference?

Cannot find the interop type that matches the embedded interop type 'Microsoft.Internal.VisualStudio.Shell.Interop.IVsColorThemeService'. Are you missing an assembly reference?

引用的程序集根本没有被触及。

所以寻找 SVsColorThemeService 反编译器在

中找到它
// Decompiled with JetBrains decompiler
// Type: Microsoft.Internal.VisualStudio.Shell.Interop.SVsColorThemeService
// Assembly: Microsoft.VisualStudio.Shell.12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MVID: B8FCA7E4-7D13-4E4B-A74B-6B6B01263DAF
// Assembly location: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VSSDK\VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Shell.12.0.dll

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Microsoft.Internal.VisualStudio.Shell.Interop
{
  [CompilerGenerated]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [TypeIdentifier]
  [Guid("0D915B59-2ED7-472A-9DE8-9161737EA1C5")]
  [ComImport]
  public interface SVsColorThemeService
  {
  }
}

如您所见,程序集已被引用。所以问题是如何掌握 SVsColorThemeServie 并以正确的方式使用它?

EDIT1:经过一些研究并遵循@Simon Mourier 的建议,我在解决方案中添加了以下代码:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Microsoft.Internal.VisualStudio.Shell.Interop
{
    [ComImport]
    [Guid("0D915B59-2ED7-472A-9DE8-9161737EA1C5")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface SVsColorThemeService
    {
    }

    [ComImport]
    [Guid("EAB552CF-7858-4F05-8435-62DB6DF60684")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IVsColorThemeService
    {
        IVsColorTheme CurrentTheme { get; }

        IVsColorThemes Themes { get; }

        void _VtblGap1_4();
        void _VtblGap2_1();
    }

    [ComImport]
    [Guid("98192AFE-75B9-4347-82EC-FF312C1995D8")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IVsColorThemes
    {
        IVsColorTheme GetThemeFromId([In] Guid ThemeId);
    }

    [ComImport]
    [Guid("413D8344-C0DB-4949-9DBC-69C12BADB6AC")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IVsColorTheme
    {
        Guid ThemeId { get; }

        void _VtblGap1_1();

        void Apply();
    }
}

它的作用是为 COM 对象提供缺少的接口。但是它仍然无法构建,抛出相同的错误 + 冲突类型的预热。

Warning 3   The type 'Microsoft.Internal.VisualStudio.Shell.Interop.IVsColorTheme' in 'c:\Users\Codemaster\Documents\Visual Studio 2013\Projects\VSPackage1\VSPackage1\Class1.cs' conflicts with the imported type 'Microsoft.Internal.VisualStudio.Shell.Interop.IVsColorTheme' in 'c:\Program Files (x86)\Microsoft Visual Studio 12.0\VSSDK\VisualStudioIntegration\Common\Assemblies\v4.0\Microsoft.VisualStudio.Shell.12.0.dll'. Using the type defined in 'c:\Users\Codemaster\Documents\Visual Studio 2013\Projects\VSPackage1\VSPackage1\Class1.cs'.    c:\users\codemaster\documents\visual studio 2013\Projects\VSPackage1\VSPackage1\Class1.cs   19  9   VSPackage1

这里有一个 Link 可以下载示例项目的地方: https://drive.google.com/file/d/0B7jBoLH-qaqTV09Mb1VCa3hld1E/view?usp=sharing

这些 VS 程序集中的某些类型需要其他程序集 (PIA)。这在这里解释:Troubleshooting Errors When Embedding Type Information (Doug Rothaus).

在这个 Visual Studio 特定案例中,除了手动重新创建类型外,我不知道如何解决它(那些程序集在哪里?)(提示:使用 .NET 反射器等工具来帮助您这很重要,因为 DLL 确实包含定义)。例如,这里是编译代码的方法,只需在某处添加:

namespace MyInterop
{
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0D915B59-2ED7-472A-9DE8-9161737EA1C5")]
    public interface SVsColorThemeService
    {
    }
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("EAB552CF-7858-4F05-8435-62DB6DF60684")]
    public interface IVsColorThemeService {
        void _VtblGap1_4();
        IVsColorThemes Themes { [return: MarshalAs(UnmanagedType.Interface)] get; }
        IVsColorNames ColorNames { [return: MarshalAs(UnmanagedType.Interface)] get; }
        IVsColorTheme CurrentTheme { [return: MarshalAs(UnmanagedType.Interface)] get; }
    }
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("98192AFE-75B9-4347-82EC-FF312C1995D8")]
    public interface IVsColorThemes {
        [return: MarshalAs(UnmanagedType.Interface)]
        IVsColorTheme GetThemeFromId([In] Guid ThemeId);
    }
    [ComImport, Guid("413D8344-C0DB-4949-9DBC-69C12BADB6AC"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IVsColorTheme {
        void _VtblGap1_1();
        IVsColorEntry this[ColorName Name] { [return: MarshalAs(UnmanagedType.Interface)] get; }
        Guid ThemeId { get; }
    }
    [ComImport, TypeIdentifier, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("BBE70639-7AD9-4365-AE36-9877AF2F973B")]
    public interface IVsColorEntry {
        ColorName ColorName { get; }
        byte BackgroundType { get; }
        byte ForegroundType { get; }
        uint Background { get; }
        uint Foreground { get; }
    }
    public struct ColorName {
        public Guid Category;
        [MarshalAs(UnmanagedType.BStr)]
        public string Name;
    }
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("92144F7A-61DE-439B-AA66-13BE7CDEC857")]
    public interface IVsColorNames {
        void _VtblGap1_2();
        int Count { get; }
        System.Collections.IEnumerator GetEnumerator();
    }
}