66 votes

Comment référencer un mot-clé C# dans une documentation XML ?

<see cref="switch" /> par exemple, ne fonctionne pas - j'obtiens l'avertissement de compilation : XML comment on ... has syntactically incorrect cref attribute 'switch'


Contexte pour ceux qui sont intéressés...

/// <summary>Provides base functionality for hand-coded abstractions of API method wrappers, mostly those that abstract over
/// parameters that are required to be JSON-encoded.</summary>
public class FacebookArgs : Dictionary<String, Object>
{
    /// <summary>Initializes an instance of <see cref="FacebookArgs" />.</summary>
    public FacebookArgs() { }

    /// <summary>Intializes an instance of <see cref="FacebookArgs" />, that contains elements copied from <paramref name="dictionary "/>.</summary>
    /// <param name="dictionary"></param>
    public FacebookArgs(IDictionary<String, Object> dictionary)
        : base(dictionary) { }

    /// <summary>Gets or sets the value associated with the specified key.</summary>
    /// <param name="key">The key of the value to get or set.</param>
    /// <returns>The value associated with the specified key.</returns>
    /// <remarks>This implementation hides the base indexer implementation such that specifying a key that does not exist returns null rather than throwing a <see cref="KeyNotFoundException" />.</remarks>
    public new Object this[String key]
    {
        get
        {
            Object value;
            if (this.TryGetValue(key, out value)) return value;
            else return null;
        }
        set { base[key] = value; }
    }

    /// <summary>In derived classes, provides specialized serialization logic for specific properties contained in this object.</summary>
    /// <param name="key">The key of the property to serialize.</param>
    /// <param name="args">A reference to a dictionary of arguments that will be passed directly to a <see cref="FacebookRequest" /> object.</param>
    /// <remarks>
    /// <para>This method allows specialized serialization logic, such as JSON encoding, to be applied to specific properties.</para>
    /// <para>To implement, use a <c>switch</c> (<c>Select</c> in VB.NET) statement to filter based on <paramref name="key" /> and provide the property-specific logic.
    /// The resulting value should then be added to <paramref name="args" /> using the same <paramref name="key "/>.
    /// </para>
    /// <para>Properties that do not require additional processing (strings, integral values, etc) should be ignored.</para>
    /// </remarks>
    protected virtual void SerializeProperty(String key, ref IDictionary<String, Object> args) { }

    /// <summary>Returns a dictionary of key/value pairs suitable to be passed a <see cref="FacebookRequest" /> object.</summary>
    /// <returns>A dictionary of key/value pairs suitable to be passed a <see cref="FacebookRequest" /> object.</returns>
    /// <remarks>This method calls the <see cref="SerializeProperty" /> for each key in the object, which allows property-specific processing
    /// to be done on any property.</remarks>
    /// <seealso cref="SerializeProperty" />
    public IDictionary<String, Object> GetArgs()
    {
        IDictionary<String, Object> args = new Dictionary<String, Object>();

        foreach (String key in this.Keys)
        {
            this.SerializeProperty(key, ref args);

            if (!args.ContainsKey(key) && this[key] != null)
            {
                args.Add(key, this[key]);
            }
        }

        return args;
    }
}

La balise en question peut être trouvée dans le <remarks> tag pour SerializeProperty . J'ai opté pour une documentation plus détaillée. Je prévois également de fournir des <example> mais je n'ai pas encore eu le temps de le faire.

104voto

Jon Skeet Points 692016

Cref est destiné à faire référence à un autre membre - une classe, une méthode, etc.

Quel lien voulez-vous qu'il établisse dans ce cas ? D'une manière générale, quel effet global souhaitez-vous obtenir ?

Selon cette excellent guide des documents XML El <see> a un attribut non documenté langword :

<see langword="switch" />

Cela vous aiderait-il ? Cela pourrait valoir la peine d'essayer, juste pour voir ce que ça donne.

Si vous voulez simplement utiliser un hyperlien normal, utilisez href au lieu de cref :

<see href="http://msdn.microsoft.com/en-us/library/06tc147t.aspx">switch</see>

0 votes

Cela fonctionne même pour VB.NET dans un projet C# ! Pour l'implémenter, utilisez un <see langword="switch" /> (<see langword="Select" /> en VB.NET)

0 votes

Je dois admettre que je n'avais jamais vu cet attribut avant. J'ai juste connu la page, et vu ce qu'elle avait à dire pour elle-même :)

2 votes

Je suppose <see>...</a> est une faute de frappe. Les deux, <see href="link"></see> y <a href="link"></a> fonctionnent pour les hyperliens normaux (testés avec Sandcastle : SHFB ).

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X