How to reference an indexer member of a class in C# comments

爱⌒轻易说出口 提交于 2019-11-29 18:09:58

问题


In order to reference a member of a class in XML comments/documentation, you have to use the following tag:

<see cref="member"/>

It is better explained here.

How do you reference an indexer?

I mean, a member like this one:

internal object this[ int index ] {
    ...
}

Thanks in advance.


回答1:


<see cref="P:System.Collections.ArrayList.Item(System.Int32)" />



回答2:


<see cref="this[int]" />



回答3:


In general, in order to find out, how to reference any member in your comments, find the member in your XML documentation file for the assembly. It is created on each build. With the only exception of generics the member reference can be taken from here:

</member>
<member name="P:My.Namespace.Class1.Item(System.String)">
    <summary>
       retrieve a single item of the given name from this instance
    </summary>
    <param name="name">name of the item</param>
    <returns>the item</returns>
</member>
<member name="M:My.Namespace.Class1.Function1(System.Int32[])">
    <summary> 
    ... 

Unfortunately, generic definition formats seem not to be compatible between the documentation file and the cref tags. While in the XML file, generics look like that:

<member name="M:My.Namespace.Class1.Get``1(System.String)">
    <summary>
    retrieve an named item of the given type
    </summary>
    <typeparam name="T">the type of the item to retrieve</typeparam>
    ...

The cref tag expects them in one of the following formats:

/// <seealso cref="M:My.Namespace.Class1.Get{T}(System.String)"/>   

/// <seealso cref="M:My.Namespace.Class1.Get&lt;T>(System.String)"/>   



回答4:


I've had the same question, but with a generic Dictionary.Item(TKey) property. The answer by leppie

<see cref="P:System.Collections.ArrayList.Item(System.Int32)" />

and the additional link by ICR (unfortunately I cannot find the "mscorlib.xml")

MSDN: Processing the XML File (C# Programming Guide)

helped me out.

But the answer by user492238
(I know, I should directly comment his answer. But since I am new and this is my first post, please go easy on me, because I am not allowed to comment due to my low reputation.)

<seealso cref="M:My.Namespace.Class1.Get{T}(System.String)"/>
<seealso cref="M:My.Namespace.Class1.Get&lt;T>(System.String)"/>

resulted only in plain, black text, whereby only the seconds shows tag signs <> as given "hard-coded".

I found the solution on the MSDN page to use backticks (`) for generics and got a full ("colory") references to the class and property within my XMLDoc:

    <see cref="P:System.Collections.Generic.Dictionary`2.Item(`0)" />
Dictionary<TKey, TValue>.this[TKey]



回答5:


<see cref="ReadOnlyCollection{T}.this[int]" />

as proposed here.



来源:https://stackoverflow.com/questions/341118/how-to-reference-an-indexer-member-of-a-class-in-c-sharp-comments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!