【问题标题】:How to Add Comments to C# 9 Records如何向 C# 9 记录添加注释
【发布时间】:2021-03-04 03:34:49
【问题描述】:

C# 9 记录具有如下的简短形式:

public record Car(int CarId, int Cylinders, string Make, string Model);

如何将文档 cmets 添加到记录的属性中?请注意,这与this 其他询问长格式的问题不同。

【问题讨论】:

    标签: c# comments record c#-9.0


    【解决方案1】:

    正确的方法看起来就这么简单:

    /// <summary>
    /// Represents a car.
    /// </summary>
    /// <param name="CarId">The id of the car.</param>
    /// <param name="Cylinders">The number of cylinders.</param>
    /// <param name="Make">The make of the car.</param>
    /// <param name="Model">The model of the car.</param>
    public record Car(int CarId, int Cylinders, string Make, string Model);
    

    当您创建此记录的新实例时,这在 IDE 的 IntelliSense(VSCode 测试)中确实有效(即new Car(...) 将为您提供不同参数的信息)。

    但是,如果您在.csproj 中启用了&lt;GenerateDocumentationFile&gt;true&lt;/GenerateDocumentationFile&gt;,编译器的警告系统本身就会出现问题。对于每个参数,您都会收到以下警告对:

    warning CS1572: XML comment has a param tag for 'CarId', but there is no parameter by that name
    warning CS1573: Parameter 'CarId' has no matching param tag in the XML comment for 'Car.Car(int, int, string, string)' (but other parameters do)
    

    所以它确实有效,但是编译器抱怨参数没有记录,并且有一个不存在的参数的文档。

    Put record parameters in scope of xml docs #49134 可能会在下一个版本中解决这个问题,希望如此。

    【讨论】:

    • 仅供参考,该错误已在 2021 年 3 月 2 日发布的 .NET 5.0.200 SDK 中解决
    • Intellisense(开始输入“参数”)并在记录上方添加 3 个斜杠时自动填充所有参数(就像您可以在类上方一样)仍然不适用于更新的 VS2019 9/2021
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多