You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS 2019项目加载时运行资源生成器的扩展实现技术问询

Great question! Let's break this down into the two parts you asked:

1. Are there existing extensions to handle this?

From what I've researched, there isn't a mainstream Visual Studio extension built specifically for this exact workflow. That said, you don't necessarily need an extension to get the behavior you want—MSBuild configuration can handle both excluding generated designer files and triggering resource generation on project load in a lightweight way:

  • To exclude *.designer.cs from your repo, mark them as generated in your project file and update your .gitignore:
    <ItemGroup>
      <EmbeddedResource Update="*.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>%(Filename).designer.cs</LastGenOutput>
      </EmbeddedResource>
      <GeneratedFiles Include="*.designer.cs" />
      <DefaultItemExcludes>$(DefaultItemExcludes);**/*.designer.cs</DefaultItemExcludes>
    </ItemGroup>
    
  • You can also force resource generation to run on project load by adding a custom MSBuild target (more on this below, even if you don't build an extension).

2. Which SDK classes/events to hook if building a custom extension?

If you want to build a dedicated extension, here are the key Visual Studio SDK points to target:

  • Project Load Event Listener:
    Use the IVsSolutionEvents3 interface (specifically its OnAfterOpenProject method) to trigger your logic immediately after a project finishes loading. Register this event in your extension's package to get notified every time a project opens in the solution.
    For more granular control over project load stages, look into IVsProjectLoadEvents, which exposes events like OnBeforeProjectLoad and OnAfterProjectLoadComplete.
  • Project File Traversal:
    To scan the project for RESX files, use the Microsoft.Build.Evaluation.Project class (reference the MSBuild assemblies in your extension) to load the project file, then iterate over its Items collection to filter for EmbeddedResource items with the .resx extension. Alternatively, use the IVsProject COM interface's GetItemCount and GetItemAt methods if you prefer working with native VS APIs.
  • Triggering Resource Generation:
    You can either:
    • Call ResGen.exe directly via a process start (passing the RESX file paths as arguments), or
    • Trigger the built-in MSBuild GenerateResource target using IVsBuildManagerAccessor to ensure compatibility with VS's build system and existing project settings.

As a quick alternative to building an extension, adding this MSBuild target to your project file will trigger resource generation on project load without any extra tools:

<Target Name="ForceResourceGenOnLoad" BeforeTargets="InitializeBuild;BeforeBuild">
  <GenerateResource
    Sources="@(EmbeddedResource)"
    OutputFiles="@(EmbeddedResource->'%(RootDir)%(Directory)%(Filename).designer.cs')"
    StronglyTypedClassName="%(Filename)"
    StronglyTypedLanguage="C#"
    StronglyTypedNamespace="$(RootNamespace)"
  />
</Target>

内容的提问来源于stack exchange,提问作者James Lavery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:46:08