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

发布于 2024-07-15 06:26:54 字数 1221 浏览 12 评论 0 原文

为谓词帮助器类编写一些 XML 文档。 但我不知道我可以引用 Expression> 而不会出现语法错误。 有可能吗? 我已经尝试过:

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

但是我在 {T, bool}} 下看到一条红色的波浪线。 但这是有效的:

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

有人有线索吗?


更新:

给出的答案(我接受了)似乎确实有效。 但现在我开始收到很多关于无法解决问题的警告。 我有一个名为 ExpressionBuilder 的类,它经常与 Expression> 配合使用。 所以我当然想在我的 XML 注释中引用这一点。

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

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

但都不起作用。 (在最后一个,ReSharper 在 {T,Boolean}} 下放置了一个蓝色的波浪线,我在使用它的所有地方的编译下都会收到两个警告,其中表示:

  1. “blah blah”的 XML 注释具有 cref 属性“Expression>” 无法解决
  2. 类型参数声明必须是标识符而不是类型。 另请参阅错误 CS0081。

在我尝试引用 Range> 的地方遇到了同样的问题(Range 也不起作用。无论是 { } 还是 < code><&gt;)

我不应该引用这些类型的泛型吗?

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:

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

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

<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.

I have tried both versions that I know about:

<see cref="Expression<Func<T, Boolean>>"/>
<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:

  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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我一直都在从未离去 2024-07-22 06:26:54

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

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

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

为了正确记录你的课程,我认为你必须写一些类似的内容:

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

我希望你喜欢文字。

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.

Lasse V Karlsen's answer made it click for me:

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.

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.

我不咬妳我踢妳 2024-07-22 06:26:54

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

文档中没有 Expression> 这样的东西,所以显然指向它的链接是行不通的。

您可以链接到 Expression 因为它存在。

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

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

但这有效:

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

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

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.

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

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

/// <see cref="Expression<Func<T>>"/>.
/// <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.

赠意 2024-07-22 06:26:54

不要使用空的 see 元素 ()。 相反,将文本放入 see 元素内

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

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

<see cref="IEnumerable{T}">IEnumerable</see><<see cref="..."/>>
記憶穿過時間隧道 2024-07-22 06:26:54
// Use "<" instead of "<" symbol and ">" instead of ">" symbol.

// Sample:

<see cref="Expression<Func<T, bool>>"/>
// Use "<" instead of "<" symbol and ">" instead of ">" symbol.

// Sample:

<see cref="Expression<Func<T, bool>>"/>
旧伤慢歌 2024-07-22 06:26:54

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

不管怎样,在 VS 2017 和 R# Ultimate 2017.1 中,这个文档注释...

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

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

List{List{byte}} 分割帧

...没有。 Eeeenteresting

还是丑? 是的。

丑陋? 我认为这比我自己使用 <> 没那么可怕......

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.

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

<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>

...does not. Eeeenteresting.

Still ugly? Yes.

As ugly? I think it's less horrible than using < and > myself....

可爱暴击 2024-07-22 06:26:54

我尝试了有关堆栈溢出的所有方法,以获得在多种情况下工作的结果。 这是一个适合我的解决方案。 (这对其他人来说是主观的。)

  1. 产生可点击的链接。
  2. 将鼠标悬停在标识符上有效。
  3. 正确生成 .xml 文件。
  4. 智能感知中不会产生错误。
  5. 适用于可为空的泛型类型参数。
  6. 适用于 Resharper 及其内置 XML 文档窗口(Resharper -> 编辑 -> 显示快速文档)
  7. 适用于 Atomineer Pro Documentaion Visual Studio Extension 的 XAM 文档预览。
  8. 适用于泛型类型的泛型类型。

示例#1

  /// <summary>
  ///  This instance field holds a reference to the
  ///  <see cref="ConcurrentDictionary{Decimal, Boolean}"/> as
  ///  <see cref="T:ConcurrentDictionary<decimal, bool?>"/> that contains
  ///  the list of all PDF's that are currently opened and being displayed.
  /// </summary>
  private ConcurrentDictionary<decimal, bool?> openedPdfs = default!;

  Note: 
    The ConcurrentDictionary{Decimal, Boolean} will correctly produce a
    clickable link of ConcurrentDictionary{TKey, TValue} on hovering while
    T:ConcurrentDictionary<decimal, bool?> makes sure the reader gets
    information on what type TKey and TValue are.

示例#2(使用“T”)

  /// <summary>
  ///  This instance field holds a reference to the
  ///  <see cref="ConcurrentDictionary{TKey, TValue}"/> as
  ///  <see cref="T:ConcurrentDictionary<decimal, bool?>"/> that contains
  ///  the list of all PDF's that are currently opened and being displayed.
  /// </summary>
  private ConcurrentDictionary<decimal, bool?> openedPdfs = default!;

I tried everything on stack overflow to a get results that work under several scenarios. Here's a solution that works for me. (That's subjective concerning anyone else.)

  1. Produces clickable links.
  2. Hovering over identifiers works.
  3. Produces .xml file correctly.
  4. Produces no errors in intellisense.
  5. Works for nullable generic type parameters.
  6. Works in Resharper and it's built-in XML Doc window (Resharper -> Edit -> Show Quick Documentation)
  7. Works in XAM Doc Preview for Atomineer Pro Documentaion Visual Studio Extension.
  8. Works with a generic type of a generic type.

Example #1

  /// <summary>
  ///  This instance field holds a reference to the
  ///  <see cref="ConcurrentDictionary{Decimal, Boolean}"/> as
  ///  <see cref="T:ConcurrentDictionary<decimal, bool?>"/> that contains
  ///  the list of all PDF's that are currently opened and being displayed.
  /// </summary>
  private ConcurrentDictionary<decimal, bool?> openedPdfs = default!;

  Note: 
    The ConcurrentDictionary{Decimal, Boolean} will correctly produce a
    clickable link of ConcurrentDictionary{TKey, TValue} on hovering while
    T:ConcurrentDictionary<decimal, bool?> makes sure the reader gets
    information on what type TKey and TValue are.

Example # 2 (using "T")

  /// <summary>
  ///  This instance field holds a reference to the
  ///  <see cref="ConcurrentDictionary{TKey, TValue}"/> as
  ///  <see cref="T:ConcurrentDictionary<decimal, bool?>"/> that contains
  ///  the list of all PDF's that are currently opened and being displayed.
  /// </summary>
  private ConcurrentDictionary<decimal, bool?> openedPdfs = default!;
梦忆晨望 2024-07-22 06:26:54

我来使用这个:

<see cref=""EqualityComparer{T}.Default"">EqualityComparer<<typeparamref name=""TSource""/>>.Default</see>

应用到你的OP这里结果:

<see cref="System.Linq.Expressions.Expression{TDelegate}">Expression<Func<<typeparamref name="TSource"/>, Boolean>>.</see>

这里是IntelliSense弹出窗口的图像:

VS2019 中评论的渲染

I came to use this:

<see cref=""EqualityComparer{T}.Default"">EqualityComparer<<typeparamref name=""TSource""/>>.Default</see>

Applied to your OP here the result:

<see cref="System.Linq.Expressions.Expression{TDelegate}">Expression<Func<<typeparamref name="TSource"/>, Boolean>>.</see>

Here the image of the IntelliSense pop-up:

Render of the comment in VS2019

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文