我可以在构造函数注释中引用 属性 注释吗?
Can I reference property comments in constructor comments?
如果我的 class 有一个注释 public 属性,它是通过构造函数分配的,我可以从具有相同名称的构造函数参数的描述中引用它的描述吗?
public class MyClass
{
/// <summary>
/// x description
/// </summary>
public int x { get; private set; }
/// <summary>
/// y description
/// </summary>
public int y { get; private set; }
/// <summary>
/// Constructor description
/// </summary>
/// <param name="x">How do I reference x description from here?</param>
/// <param name="y">And y description?</param>
public MyClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
您不能 包含 描述,但您可以 link 使用 <see>
标签 属性 文档。例如:
<param name="x">The initial value for <see cref="x"/></param>
顺便说一句,我强烈建议您遵循 .NET 命名约定,其中 public 成员以大写字母开头。
这可以使用 <inheritdoc/>
,特别是通过使用可选的 cref
和 path
属性。
注意:我不确定它可用的语言或框架版本,它适用于我使用 .NET 5 / C# 9。如果有人对起始版本发表评论,我可以编辑。
参考:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc
对于您的示例(不同之处在于构造函数 XML 注释中的 param
元素):
public class MyClass
{
/// <summary>
/// x description
/// </summary>
public int x { get; private set; }
/// <summary>
/// y description
/// </summary>
public int y { get; private set; }
/// <summary>
/// Constructor description
/// </summary>
/// <param name="x"><inheritdoc cref="x" path='/summary'/></param>
/// <param name="y"><inheritdoc cref="y" path='/summary'/></param>
public MyClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
这将使构造函数参数继承您的参数的摘要注释,减少冗余。
如果我的 class 有一个注释 public 属性,它是通过构造函数分配的,我可以从具有相同名称的构造函数参数的描述中引用它的描述吗?
public class MyClass
{
/// <summary>
/// x description
/// </summary>
public int x { get; private set; }
/// <summary>
/// y description
/// </summary>
public int y { get; private set; }
/// <summary>
/// Constructor description
/// </summary>
/// <param name="x">How do I reference x description from here?</param>
/// <param name="y">And y description?</param>
public MyClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
您不能 包含 描述,但您可以 link 使用 <see>
标签 属性 文档。例如:
<param name="x">The initial value for <see cref="x"/></param>
顺便说一句,我强烈建议您遵循 .NET 命名约定,其中 public 成员以大写字母开头。
这可以使用 <inheritdoc/>
,特别是通过使用可选的 cref
和 path
属性。
注意:我不确定它可用的语言或框架版本,它适用于我使用 .NET 5 / C# 9。如果有人对起始版本发表评论,我可以编辑。
参考:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc
对于您的示例(不同之处在于构造函数 XML 注释中的 param
元素):
public class MyClass
{
/// <summary>
/// x description
/// </summary>
public int x { get; private set; }
/// <summary>
/// y description
/// </summary>
public int y { get; private set; }
/// <summary>
/// Constructor description
/// </summary>
/// <param name="x"><inheritdoc cref="x" path='/summary'/></param>
/// <param name="y"><inheritdoc cref="y" path='/summary'/></param>
public MyClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
这将使构造函数参数继承您的参数的摘要注释,减少冗余。