首页 文章

单个文件生成器不适用于Visual Studio 2017中的.NET标准项目

提问于
浏览
3

我已经基于模板[1](编译成可安装的VSIX输出,包括组件的自动注册)实现了单文件生成器,并且:

  • 适用于VS 2015和VS2017中的经典.NET项目;

  • 适用于VS2017中的.NET Core项目;

  • 但不适用于VS2017中的.NET Standard项目 .

example

所有 HasCustomTool.xml 文件都具有相同的配置,所有这些文件都指定了'Custom Tool'属性 .

当我查看 .csproj 文件时,我可以看到它们是不同的 . DotNetCore.csproj 文件的(工作)内容是:

<ItemGroup>
    <Compile Update="HasCustomTool.cs">
      <DependentUpon>HasCustomTool.xml</DependentUpon>
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <None Update="HasCustomTool.xml">
      <LastGenOutput>HasCustomTool.cs</LastGenOutput>
      <Generator>PtResxErrorTool</Generator>
    </None>
  </ItemGroup>

DotNetStandard.csproj 文件具有:

<ItemGroup>
      <None Update="HasCustomTool.xml">
          <LastGenOutput>HasCustomTool.cs</LastGenOutput>
          <Generator>PtResxErrorTool</Generator>
      </None>
  </ItemGroup>

当您将标记从 DotNetCore.csproj 复制到 DotNetStandard.csproj (手动)时,您将获得所需的结构 - 但永远不会激活生成器 .

有没有人为.NET Standard项目成功编写了VSIX单文件生成器?关于如何调试此问题的任何指针?

[1] https://github.com/Microsoft/VSSDK-Extensibility-Samples/tree/master/Single_File_Generator

1 回答

  • 4

    您需要向您的 class 添加新的CodeGeneratorRegistration .

    “{9A19103F-16F7-4668-BE54-9A1E7A4F7556}”

    在我的情况下,我的 class 看起来像

    [ComVisible(true)]
    [Guid(GuidList.GuidI18NReactivetring)]
    [ProvideObject(typeof(I18NReactive))]
    [CodeGeneratorRegistration(typeof(I18NReactive), "I18N.Reactive", vsContextGuids.vsContextGuidVCSProject, GeneratesDesignTimeSource = true)]
    [CodeGeneratorRegistration(typeof(I18NReactive), "I18N.Reactive", "{9A19103F-16F7-4668-BE54-9A1E7A4F7556}", GeneratesDesignTimeSource = true)]
    public class I18NReactive : IVsSingleFileGenerator, IObjectWithSite
    {
    }
    

    源信息来自这个帖子

    https://github.com/aspnet/Tooling/issues/394

相关问题