.NET (Core) 上的 c# 嵌入资源中的 Xml Doc 注释

问题描述

我有一个从 .NET 框架迁移到 .NET Core(然后是 .NET 5)的 c# 项目。

我们已经好几年没有碰过 .resx 文件了,但是现在我更新了 .resx 文件Resources.Designer.cs 文件重新生成了(好),删除之前包含的所有 Xml Doc(不好,会产生很大的差异,还会丢失信息)。

如何指示我的 Resx 构建步骤像过去一样保留/生成 XML 文档?

最初这段代码是用 Windows 上的 Visual Studio 编写和生成的,我们现在在 Mac 上使用 Rider

编辑:看起来它不是特定于 .NET 5,而是 Windows+VS 与 Mac+Rider 配对,因为我团队中的 Windows 开发人员在我的更改之上重新生成了这些评论

如何在没有 Visual Studio 的情况下在 Mac/Linux 上获得此功能

文件部分:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Resources {
    using System;
    
    
    /// <summary>
    ///   A strongly-typed resource class,for looking up localized strings,etc.
    /// </summary>
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member,edit your .ResX file then rerun ResGen
    // with the /str option,or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder","16.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    public class Resources {

...

        /// <summary>
        ///   Looks up a localized string similar to About.
        /// </summary>
        public static string About {
            get {
                return ResourceManager.GetString("About",resourceCulture);
            }
        }

文件部分:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Resources {
    using System;
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder","16.0.0.0")]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    public class Resources {

...

        public static string About {
            get {
                return ResourceManager.GetString("About",resourceCulture);
            }
        }

有问题的文件从 12k LOC 减少到 8k LOC,丢失了很多注释,并且使事情变得更难处理。

解决方法

我不知道这是否适合您作为解决方案,但我的建议是,当出现操作系统/工具问题时,启动 Docker 并从 Windows Docker 映像手动执行 MSBUILD 命令。我建议这样做,因为您的 WinDev 同事正在重新生成丢失的评论。

MSBUILD Commands

想到的唯一一个不起作用的问题是下面链接的那个,他们仍然需要指定配置类型,并且项目(.CSPROJ 文件)被设置为“构建”文档配置。文档生成是“构建”和 Visual Studio“配置”特定的。

例如,如果在项目构建启动时构建了文档,并将“调试”和“任何 CPU”设置为配置,那么您需要在 MSBUILD CLI 中使用必要的参数调用相同的文件,例如>

看起来有点像这样(根据粗略的记忆)。

msbuild.exe MyProject.csproj `
/p:Configuration=Debug `
/p:Platform="Any CPU" `
/p:GenerateDocumentation `
/p:DocumentationFile="MyProject.xml"

Stackoverflow - MSBUILD XML Docs

这一切都可以编写脚本,因此它几乎可以启动一个 Docker 映像,编写生成文档的 POSH 脚本,然后将文档复制到本地主机。有点笨重,但只有当你全部完成并准备好将其推上时才需要这样做。