【问题标题】:XML Comments - How do you reference a dictionary indexer properly?XML 注释 - 如何正确引用字典索引器?
【发布时间】:2011-07-15 02:05:27
【问题描述】:

顾名思义,我不知道如何引用字典索引器。这里有什么帮助吗? :)

仅供参考,我试过了:

<see cref="Item"/>
<see cref="Item(Int32)"/> //Highly doubted this would work.
<see cref="Item(TKey)"/>
<see cref="Item[TKey]"/>

【问题讨论】:

    标签: c# dictionary xml-comments indexer


    【解决方案1】:

    您可以使用完整的属性语法来引用索引器:

    namespace ConsoleApplication1
    {
        /// <summary>
        /// See indexer <see cref="P:ConsoleApplication1.MyDictionary`2.Item(`0)"/>
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        public class MyDictionary<TKey, TValue>
        {
            /// <summary>
            /// Indexer
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public TValue this[TKey key]
            {
                get { return default(TValue); }
                set { }
            }
        }
    }
    

    您可以通过检查生成的 XML 文件来检查该属性是否已正确解析:

    <doc>
        <assembly>
            <name>ConsoleApplication1</name>
        </assembly>
        <members>
            <member name="T:ConsoleApplication1.MyDictionary`2">
                <summary>
                See <see cref="P:ConsoleApplication1.MyDictionary`2.Item(`0)"/>
                </summary>
                <typeparam name="TKey"></typeparam>
                <typeparam name="TValue"></typeparam>
            </member>
            <member name="P:ConsoleApplication1.MyDictionary`2.Item(`0)">
                <summary>
                Indexer
                </summary>
                <param name="key"></param>
                <returns></returns>
            </member>
        </members>
    </doc>
    

    注意第一个 P: 是如何匹配第二个的。

    最后,确保它与 Intellisense 一起使用:


    由原始海报更新(myermian):

    我进行了一些挖掘,发现索引器属性的缩写形式就是“this”。例如:&lt;see cref="this"/&gt;

    【讨论】:

    • 你能告诉我它的简写版本是什么吗?我的其余代码都使用速记版本,所以这个 long long 值会像大拇指一样突出。
    • 根据经验,我找不到正确解析的索引器的简短形式。长格式允许您指定任意成员。如果有一个简短的形式可以解决,我同意它会更优雅和方便。
    • 嗯,我敢肯定有(一切都有一个简短的形式)。它只是想弄清楚它是什么。通常,这很容易。但是,这个索引器让我很痛苦,哦,痛苦。
    • 请更新答案以包含缩写为&lt;see cref="this"/&gt;。是的,只需一个 this 就可以正常展开。花了一段时间才发现简短的形式。现在看起来如此合乎逻辑和简单。 :)
    【解决方案2】:

    试试&lt;see cref="P:Item(System.Int32)" /&gt;(名称是Item,而不是Items

    【讨论】:

    • 我的代码正确,在这里输入错误(并复制/粘贴)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 2016-11-23
    • 2011-05-18
    相关资源
    最近更新 更多