c# - 如何向 C# 9 记录添加注释
问题描述
C# 9 记录有一个简短的形式,如下所示:
public record Car(int CarId, int Cylinders, string Make, string Model);
如何将文档注释添加到记录的属性中?请注意,这与询问长格式的其他问题不同。
解决方案
正确的方法似乎很简单:
/// <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);
new Car(...)
当您创建此记录的新实例时(即将为您提供不同参数的信息),这在 IDE 的 IntelliSense(VSCode 测试)中确实有效。
但是,如果您<GenerateDocumentationFile>true</GenerateDocumentationFile>
在.csproj
. 对于每个参数,您将收到以下警告对:
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)
所以它确实有效,但是编译器既抱怨参数没有记录,也有一个不存在的参数的文档。
将记录参数放在 xml 文档 #49134 的范围内可能会在下一个版本中解决该问题,希望如此。
推荐阅读
- layout - 评论后添加附加标签
- javascript - 无限自动播放纯 CSS 轮播
- angular - Angular 7 中有没有办法在编译时决定使用哪个服务?
- r - 如何将字符串因子转换为日期?
- r - 在ggplot2中将锯齿线转换为样条线
- reactjs - 在 Redux 操作中获取 response.Token
- c# - Windows 7 上 WinForms 应用程序中“粘贴”命令的行为(法语)
- scala - MockitoSerializationIssue:反序列化 Mockito 模拟时找不到类
- python - 使用自定义 mod_wsgi 和 python 对共享服务器的访问受限
- javascript - 从 D3 v3 升级到 v5 时在 d3-legend 中使用 d3.format 时出错