在 C# XML 文档中引用泛型类型的泛型类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/684982/
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-04 13:22:44  来源:igfitidea点击:

Referring to a generic type of a generic type in C# XML documentation?

c#visual-studio-2008genericsresharperxml-documentation

提问by Svish

Writing some XML documentation for a predicate helper class. But I can't figure out I can refer to an Expression<Func<T, bool>>without getting a syntax error. Is it even possible? I have tried this:

为谓词助手类编写一些 XML 文档。但是我不知道我可以参考 anExpression<Func<T, bool>>而不出现语法错误。甚至有可能吗?我试过这个:

<see cref="Expression{Func{T, bool}}"/>

But I get a red squiggly line under {T, bool}}. This works though:

但是我在{T, bool}}. 这虽然有效:

<see cref="Expression{TDelegate}"/>

Anyone have a clue?

有人有线索吗?



Update:

更新:

The answer that was given (and I accepted) seemingly did work. But now I have started to get a lot of warnings about stuff not being able to resolve. I have a class called ExpressionBuilder<T>which works with Expression<Func<T, bool>>a lot. So I of course want to refer to that in my XML comments.

给出的答案(我接受了)似乎确实有效。但是现在我开始收到很多关于无法解决的问题的警告。我有一个叫做的类ExpressionBuilder<T>,它Expression<Func<T, bool>>有很多作用。所以我当然想在我的 XML 注释中引用它。

I have tried both versions that I know about:

我已经尝试了我所知道的两个版本:

<see cref="Expression&lt;Func&lt;T, Boolean&gt;&gt;"/>
<see cref="Expression{Func{T, Boolean}}"/>

But neither work. (And on the last one, ReSharper puts a blue squiggly under {T,Boolean}}I get two warnings under compilation everywhere I have used it which says that:

但两者都不起作用。(在最后一个,ReSharper 在{T,Boolean}}我使用它的任何地方都在编译时出现了两个警告,上面写着一个蓝色的波浪线:

  1. XML comment on 'blah blah' has cref attribute 'Expression>' that could not be resolved
  2. Type parameter declaration must be an identifier not a type. See also error CS0081.
  1. 'blah blah' 上的 XML 注释具有无法解析的 cref 属性 'Expression>'
  2. 类型参数声明必须是标识符而不是类型。另请参阅错误 CS0081。

Have the same issue somewhere I tried to refer to Range<Nullable<DateTime>>(Range<DateTime?>didnt work either. Both with { } and with &lt; &gt;)

在我尝试参考的某个地方遇到了同样的问题Range<Nullable<DateTime>>(也Range<DateTime?>没有工作。都带有 { } 和&lt; &gt;

Am I not supposed to refer to these kinds of generics?

我不应该提到这些类型的泛型吗?

采纳答案by Rory MacLeod

There seems to be no way to refer to a generic of a generic in XML documentation, because actually, there's no way to refer to a generic of any specific type.

似乎无法在 XML 文档中引用泛型的泛型,因为实际上,无法引用任何特定类型的泛型。

Lasse V Karlsen's answermade it click for me:

Lasse V Karlsen 的回答让我觉得很有趣:

If you write <see cref="IEnumerable{Int32}" />, the compiler just uses "Int32" as the type parameter name, not the type argument. Writing <see cref="IEnumerable{HelloWorld}" />would work just as well. This makes sense because there is no specific page in MSDN for "IEnumerable of int" that your documentation could link to.

如果您编写<see cref="IEnumerable{Int32}" />,编译器仅使用“Int32”作为类型参数名称,而不是类型参数。写作<see cref="IEnumerable{HelloWorld}" />也同样有效。这是有道理的,因为 MSDN 中没有您的文档可以链接到的“IEnumerable of int”的特定页面。

To document your class properly, I think you'd have to write something like:

为了正确记录您的课程,我认为您必须编写如下内容:

<summary>
Returns an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{T,U}" /> 
of <see cref="String" />, <see cref="Int32" />.
</summary>

I hope you like text.

我希望你喜欢文字。

回答by Konstantin Tarkus

// Use "&lt;" instead of "<" symbol and "&gt;" instead of ">" symbol.

// Sample:

<see cref="Expression&lt;Func&lt;T, bool&gt;&gt;"/>

回答by Lasse V. Karlsen

What exactly would you like it to link to?

您到底希望它链接到什么内容?

There's no such thing in the documentation as a Expression<Func<T>>, so obviously a link to that would not work.

文档中没有像 a 这样的东西Expression<Func<T>>,所以显然指向它的链接不起作用。

You can link to Expression<TDelegate>because that exists.

您可以链接到,Expression<TDelegate>因为它存在。

As for what works or not, neither of the following works in Visual Studio 2008 / .NET 3.5 for me:

至于什么有效或无效,以下在 Visual Studio 2008 / .NET 3.5 中对我来说都不起作用:

/// <see cref="Expression&lt;Func&lt;T&gt;&gt;"/>.
/// <see cref="Expression{Func{T}}"/>.

But this works:

但这有效:

/// <see cref="Expression{T}"/>.

so apparently the generic type parameter doesn't have to the same as the one in the declaration.

所以显然泛型类型参数不必与声明中的参数相同。

回答by Mike Loux

I'm running into this now, as I have a function that returns a List<List<byte>>. Yeah, it's ugly, but I didn't write it. Standard disclaimer, I know.

我现在遇到了这个问题,因为我有一个返回List<List<byte>>. 是的,它很丑,但我没有写它。标准免责声明,我知道。

Anyway, in VS 2017 with R# Ultimate 2017.1, this doc comment...

无论如何,在带有 R# Ultimate 2017.1 的 VS 2017 中,此文档注释...

<returns><see cref="List{List{Byte}}" /> of split frames</returns>

...gives me a syntax error. However, this...

...给我一个语法错误。然而,这...

<returns><see><cref>List{List{byte}}</cref></see> of split frames</returns>

<returns><see><cref>List{List{byte}}</cref></see> of split frames</returns>

...does not. Eeeenteresting.

...才不是。 Eeeenteresting

Still ugly? Yes.

还是丑?是的。

Asugly? I think it's less horrible than using &lt;and &gt;myself....

由于丑吗?我认为它没有使用&lt;&gt;我自己那么可怕......

回答by ILMTitan

Don't use an empty see element (<see cref="..." />). Instead, put text inside the see element

不要使用空的 see 元素 ( <see cref="..." />)。相反,将文本放在 see 元素中

<see cref="IEnumerable{T}">IEnumerable</see>&lt;<see cref="..."/>$gt;