在Intellisense中为静态方法显示XML注释通用参数

时间:2014-05-01 15:36:42

标签: c# visual-studio-2010 xml-comments

我无法获取以下两种方法的通用参数来显示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;
        }
    }
}

我搜索并发现了一些提示,但尝试了几件没有运气的事情。我能得到的最好的就是在Intellisence气球中显示“T”,这样就不用了...

修改

这是微软在类级别类型上执行它而不是构造函数...所以这对于方法/构造函数是否可行? (理想情况下,我希望显示内容与我的评论内联,但下面的示例图像中的方式也完全可以接受)

Microsoft Can do it

1 个答案:

答案 0 :(得分:1)

尽管这是一个非常老的问题,但我今天发现自己一直在为完全相同的问题而苦苦挣扎-我很遗憾地说,这看起来并没有一个真正的解决方案。

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

CS1584 XML注释在语法上具有不正确的cref属性'Dictionary {int,string}'

当鼠标悬停在绿色的花体上时,VS将显示一条工具提示(包括其他内容):

类型参数声明必须是标识符而不是类型。另请参见error CS0081

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

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