【问题标题】:Is there a way to reference const parameters in C# block comments?有没有办法在 C# 块注释中引用 const 参数?
【发布时间】:2016-10-29 20:28:57
【问题描述】:

在c#块cmets中,我想说的是,特定参数的默认值是一个类的const属性。有没有办法直接引用该参数?

我想在生成的文档中显示值,或者以某种结构化方式链接到属性。

这是我正在尝试做的一个例子:

public class Foo
{
    private const int DefaultBar = 20;

    ///<summary>
    ///Does the thing.
    ///</summary>
    ///<param name="bar">Description of bar. Defaults to [[DefaultBar]]</param>
    public int DoTheThing(int bar = DefaultBar)
    {
        return bar;
    }
}

上面的[[DefaultBar]] 是引用 DefaultBar 属性所需的任何语法。

因为它是一个常数,我觉得应该有一种方法可以在生成的文档中引用它,而无需手动保持它们同步。 (我不想只是用20 替换[[DefaultBar]],以防我以后想将20 更改为其他int)

我查看了C# "Constant Objects" to use as default parameters,但该问题(和相关答案)没有显示文档。

【问题讨论】:

标签: c# documentation code-documentation


【解决方案1】:

您可以像任何其他代码成员一样使用 注释标记来引用常量。在您的情况下,它将是:

public class Foo
{
    private const int DefaultBar = 20;

    ///<summary>
    ///Does the thing.
    ///</summary>
    ///<param name="bar">Description of bar. Defaults to <see cref="DefaultBar"/>.</param>
    public int DoTheThing(int bar = DefaultBar)
    {
        return bar;

    }
}

当您从这些 cmets 生成文档时,您将获得指向常量页面的链接。例如,使用VSdocman(我们的产品)生成的输出将如下所示:

【讨论】:

  • 如果我想将该常量作为其值集成到注释中,这将无济于事。例如,在“默认情况下,x 的值每 {{SOME_VALUE}} 秒更新一次”之类的文本中。
【解决方案2】:

IntelliSense 会自动为您跟踪。像你已经做的那样在注释中保留变量名称可能是一个好习惯,但是当输入对该方法的引用时,它将显示如下:

因此,每当您更改 DefaultBar 的值时,IntelliSense 都会自行提取更新后的值并将其放入工具提示中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-13
    • 2012-11-04
    • 2011-08-02
    • 2021-07-25
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多