How to reference an indexer member of a class in C

2020-02-28 05:37发布

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.

5条回答
Explosion°爆炸
2楼-- · 2020-02-28 06:12
<see cref="P:System.Collections.ArrayList.Item(System.Int32)" />
查看更多
迷人小祖宗
3楼-- · 2020-02-28 06:12

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楼-- · 2020-02-28 06:18
<see cref="ReadOnlyCollection{T}.this[int]" />

as proposed here.

查看更多
萌系小妹纸
5楼-- · 2020-02-28 06:30

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]
查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-28 06:38
<see cref="this[int]" />
查看更多
登录 后发表回答