使用带有 params 语法的 C# 的 XML 注释 cref 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/761104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:11:46  来源:igfitidea点击:

Using C#'s XML comment cref attribute with params syntax

c#xml-commentsparams-keyword

提问by Matt H

In C#, I am trying to use <see cref="blah"/> to reference a method signature that contains the params keyword. I know this converts the parameter list to an array, but I can't even figure out how to refer to an array in a CREF attribute. I am finding nothing in my searches and no one I know has any idea, either. The compiler is choking on the square brackets. I've tried all kinds of different combinations, using curly braces, using the Array class, but nothing is working. Does anyone know this?

在 C# 中,我尝试使用 <see cref="blah"/> 来引用包含 params 关键字的方法签名。我知道这会将参数列表转换为数组,但我什至不知道如何在 CREF 属性中引用数组。我在搜索中一无所获,我认识的人也没有任何想法。编译器在方括号上窒息。我尝试了各种不同的组合,使用花括号,使用 Array 类,但没有任何效果。有人知道吗?

采纳答案by jonp

The ECMA 334 Standard PDF, Annex E contains a decent overview of XML Documentation comments. You can download the standard at:

ECMA 334 标准 PDF,附件 E 包含对 XML 文档注释的全面概述。您可以在以下位置下载标准:

http://www.ecma-international.org/publications/standards/Ecma-334.htm

http://www.ecma-international.org/publications/standards/Ecma-334.htm

Specifically, you'll want section E.3.1, starting on page 496.

具体来说,您需要 E.3.1 部分,从第 496 页开始。

Similar content is also at MSDN (though MSDN seems to have terrible navigation on this topic, making it difficult to find the other sections):

MSDN 上也有类似的内容(尽管 MSDN 似乎对这个主题的导航很糟糕,很难找到其他部分):

http://msdn.microsoft.com/en-us/library/aa664787(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa664787(VS.71).aspx

The equivalent to E.3.1:

相当于 E.3.1:

http://msdn.microsoft.com/en-us/library/aa664807(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa664807(VS.71).aspx

You may also find Mono's documentation useful:

您可能还会发现 Mono 的文档很有用:

http://www.go-mono.com/docs/index.aspx?tlink=29@man%3amdoc(5)

http://www.go-mono.com/docs/index.aspx?tlink=29@man%3amdoc(5)

Specfically, the "CREF FORMAT" section covers the ID string conventions.

具体来说,“CREF FORMAT”部分涵盖了 ID 字符串约定。

Update 2018/05/23

更新 2018/05/23

The URL for the ECMA-334 standard PDF above links to the latest edition of the standard. In 2009, that was the 4th edition of the standard. However, as of December 2017, the 5th edition is current, and section E.3.1 from the 4th edition became section D.4.2 in the 5th edition.

上面 ECMA-334 标准 PDF 的 URL 链接到该标准的最新版本。2009 年,这是该标准的第 4 版。但是,截至 2017 年 12 月,第 5 版是最新版本,第 4 版的 E.3.1 节变为第 5 版的 D.4.2 节。

The previous versions of the ECMA-334 standard are available for download from the following page: https://www.ecma-international.org/publications/standards/Ecma-334-arch.htm

ECMA-334 标准的先前版本可从以下页面下载:https: //www.ecma-international.org/publications/standards/Ecma-334-arch.htm

回答by Martin Brown

You just leave out the param keyword and put in the type like this:

您只需省略 param 关键字并输入如下类型:

/// <summary>
/// <see cref="Method(string[])"/>
/// </summary>
public static void Main()
{
    Method("String1", "String2");
}

public static void Method(params string[] values)
{
    foreach (string value in values)
    {
        Console.WriteLine(value);
    }
}

回答by Olivier Dagenais

According to the B.3.1 ID string formatarticle, referencing an array is done with [square brackets] (with optional lowerbound:sizespecifiers) but if you just want to refer to an array of a certain type (or even an Object array), you can't just write

根据B.3.1 ID 字符串格式文章,引用数组是用 [方括号](带有可选lowerbound:size说明符)完成的,但是如果您只想引用某种类型的数组(甚至是 Object 数组),则可以不只是写

<see cref="Object[]"/>

<see cref="Object[]"/>

instead you need to specify you're making a type reference with the T:prefix, like

相反,您需要指定您正在使用T:前缀进行类型引用,例如

<see cref="T:Object[]"/>

<see cref="T:Object[]"/>

This does not seem to apply when referencing a specific overload of a method, such as

这在引用方法的特定重载时似乎并不适用,例如

<seealso cref="String.Join(String, String[])"/>

<seealso cref="String.Join(String, String[])"/>