gpt4 book ai didi

c# - 在静态方法的 Intellisense 中显示 XML 注释通用参数

转载 作者:行者123 更新时间:2023-12-02 03:33:09 24 4
gpt4 key购买 nike

我在获取以下两种方法的通用参数以在 Intellisence 中显示类型时遇到问题。

对于IEnumerable<T>我只是希望它显示为 double .

Intellisence screenshot for IEnumerable<T> Method

对于 IDictionary<TKey,TValue>过载,我希望它显示KeyValuePair<int,string>但当然不需要对类型进行硬编码。

Intellisence screenshot for KeyValuePair<TKey, TValue> Method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Common.FluentValidation
{
public static partial class Validate
{
/// <summary>
/// Compares two dictionaries are null or contain equal sets of items.
/// Returns true if both instances are null or contain equal sets of <see cref="T:System.Collections.Generic.KeyValuePair'1{TKey}{TValue}"></see> items; otherwise, false.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="A">The first instance to compare</param>
/// <param name="B">The second instance to compare</param>
/// <returns>true if both instances are null or contain equal sets of <see cref="T:System.Collections.Generic.KeyValuePair'1{TKey}{TValue}"></see> items; otherwise, false.</returns>
public static bool AreBothNullOrEqualSets<TKey, TValue>(IDictionary<TKey, TValue> A, IDictionary<TKey, TValue> B)
{
// XOR for null
if ((A == null) ^ (B == null))
return false;

// Compare each value in set
if (A != null)
if (!A.OrderBy(x => x.Key).SequenceEqual(B.OrderBy(x => x.Key)))
return false;

return true;
}

/// <summary>
/// Compares two sequences are null or contain equal sets of items.
/// Returns true if both instances are null or contain equal sets of <see cref="T:Common.FluentValidation.AreBothNullOrEqualSets`1"/> items; otherwise, false.
/// </summary>
/// /// for more information.
/// <typeparam name="T">The type of the enumerable.</typeparam>
/// <param name="A">The first instance to compare</param>
/// <param name="B">The second instance to compare</param>
/// <returns>true if both instances are null or contain equal sets of <see cref="T:Common.FluentValidation.AreBothNullOrEqualSets`1"/> items; otherwise, false.</returns>
public static bool AreBothNullOrEqualSets<T>(IEnumerable<T> A, IEnumerable<T> B)
{
// XOR for null
if ((A == null) ^ (B == null))
return false;

// Compare each value in set
if (A != null)
if (!A.SequenceEqual(B))
return false;

return true;
}
}
}

我搜索并找到了一些提示,但尝试了几件事但没有运气。我能得到的最好的结果就是它只在情报气球中显示“T”,这还有待改进......

编辑:

这是 Microsoft 在类级别类型参数上执行的操作...但不是在构造函数上...那么这对于方法/构造函数来说可能吗? (理想情况下,我想显示与我的评论内联的类型,但下面示例图像中的方式也是完全可以接受的)

Microsoft Can do it

最佳答案

虽然这是一个非常古老的问题,但我今天发现自己正在努力解决完全相同的问题 - 我很遗憾地说,它看起来并没有真正的解决方案。

使用 <see cref="Dictionary{int, string}">将导致以下警告(至少在 VS 2017 上):

CS1584 XML comment has syntactically incorrect cref attribute 'Dictionary{int, string}'

当鼠标悬停在绿色曲线上时,VS 将显示一个工具提示(除其他内容外):

Type parameter declaration must be an identifier not a type. See also error CS0081.

我想出的唯一解决方案就是简单地添加几个段落来指定TKey的类型。和TValue :

/// <summary>
/// Gets a <see cref="Dictionary{TKey, TValue}"/> that maps ints to strings.
/// <para>
/// TKey is <see cref="int"/>.
/// </para>
/// <para>
/// TValue is <see cref="string"/>.
/// </para>
/// </summary>
public Dictionary<int, string> Map {get;}

情报会这样显示:

Gets a Dictionary<TKey, TValue> that maps ints to strings.
TKey is int.
TValue is string.

关于c# - 在静态方法的 Intellisense 中显示 XML 注释通用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23411024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com