英文:
How do I generate .Designer.cs files from resx files as part of dotnet build
问题
.Designer.cs文件是由Visual Studio或Rider(可能也包括其他IDE)自动生成的,但它不是构建过程的一部分。我想在构建过程中使用dotnet build生成这些文件。这样我就不必将这些生成的文件放入源代码控制,以避免造成许多冲突。如何实现这一点?
英文:
.Designer.cs files are generated automatically by Visual Studio or Rider (and probably other IDEs), but it is not a part of build process. I want to generate these files as a part of build process using dotnet build. That way I don't have to put these generated files to source control where they cause a lot of conflicts. How to achieve this?
答案1
得分: 0
模板和示例中的原始包含在 .csproj 项目文件中如下所示:
<ItemGroup>
<EmbeddedResource Update="Resources\Resource.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
这会在 IDE 中生成 Resource.Designer.cs 文件,类似于问题中描述的使用 PublicResXFileCodeGenerator。
要在构建过程中生成文件,需要进行更多配置,你需要知道生成的类和命名空间的名称:
<EmbeddedResource Update="Resources\Resource.resx">
<Generator>MSBuild:Compile</Generator>
<StronglyTypedFileName>
$(IntermediateOutputPath)\Resource.Designer.cs
</StronglyTypedFileName>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedNamespace>Example.Resources</StronglyTypedNamespace>
<StronglyTypedClassName>Resource</StronglyTypedClassName>
</EmbeddedResource>
这里的生成器是构建过程,StronglyTypeFileName 指定生成 .Designer.cs 文件的路径(obj-文件夹),使用的语言(在我的情况下是 C#),最后是命名空间和类名。如果你有多个资源文件,例如不同语言的资源文件,你可能会将 Resource_en 作为英语的类名。
解决方案改编自 https://github.com/dotnet/msbuild/issues/2272。我还找到了一篇描述该解决方案的好博文,链接在这里 https://www.paraesthesia.com/archive/2022/09/30/strongly-typed-resources-with-net-core/。
英文:
The original inclusion in templates and examples in .csproj project files looks like this:
<ItemGroup>
<EmbeddedResource Update="Resources\Resource.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
This generates the Resource.Designer.cs in IDEs like described in the question using PublicResXFileCodeGenerator.
To generate the file in build process requires a bit more configuration, and you have to know the generated class and namespace names:
<EmbeddedResource Update="Resources\Resource.resx">
<Generator>MSBuild:Compile</Generator>
<StronglyTypedFileName>
$(IntermediateOutputPath)\Resource.Designer.cs
</StronglyTypedFileName>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedNamespace>Example.Resources</StronglyTypedNamespace>
<StronglyTypedClassName>Resource</StronglyTypedClassName>
</EmbeddedResource>
Here the generator is the build process, StronglyTypeFileName tells the path where the .Designer.cs file is generated to (obj-folder), language that is used (in my case C#) and finally the namespace and class name. If you have multiple resource files for instance for different languages, you might have Resource_en as a class name for English.
Solution adapted from https://github.com/dotnet/msbuild/issues/2272
I also found a good blog post describing the solution here https://www.paraesthesia.com/archive/2022/09/30/strongly-typed-resources-with-net-core/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论