C# 如何记录类属性

C# 如何记录类属性,c#,xml,documentation,C#,Xml,Documentation,类属性的XML注释的正确语法是什么?安装此选项: 右键单击属性,选择“记录此”。 填空。根据MSDN,似乎没有类属性的正式标记。但是,我会用这样的方式: /// <summary>Here is an example of a propertylist: /// <list type="Properties"> /// <item> /// <description>Property 1.</description> /// </

类属性的XML注释的正确语法是什么?

安装此选项:

右键单击属性,选择“记录此”。 填空。

根据MSDN,似乎没有类属性的正式标记。但是,我会用这样的方式:

/// <summary>Here is an example of a propertylist:
/// <list type="Properties">
/// <item>
/// <description>Property 1.</description>
/// </item>
/// <item>
/// <description>Property 2.</description>
/// </item>
/// </list>
/// </summary>
///以下是propertylist的示例:
/// 
/// 
///财产1。
/// 
/// 
///财产2。
/// 
/// 
/// 

我建议使用。它不仅强制(对我来说有点过于强烈)您进行评论,而且还提示您如何开始评论。

为了响应对显式示例的请求,以下摘录来自StyleCop帮助文档,“SA1623:PropertySummaryDocumentationMustMatchAccessors”:

属性的摘要文本必须以描述属性中公开的访问器类型的措辞开头。如果属性仅包含get访问器,则摘要必须以单词“get”开头。如果属性仅包含集合访问器,则摘要必须以单词“集合”开头。如果属性同时公开get和set访问器,则摘要文本必须以“get或set”开头

/// <summary>
/// Gets or sets the name of the customer. 
/// </summary>
public string Name
{
    get { return this.name; }
    set { this.name = value; }
}

例如,考虑下面的属性,它公开GET和SET访问器。摘要文本以“获取或设置”开头

/// <summary>
/// Gets or sets the name of the customer. 
/// </summary>
public string Name
{
    get { return this.name; }
    set { this.name = value; }
}
//
///获取或设置客户的名称。
/// 
公共字符串名
{
获取{返回this.name;}
设置{this.name=value;}
}
如果该属性返回布尔值,则会应用其他规则。布尔属性的摘要文本必须包含单词“获取一个指示是否的值”、“设置一个指示是否的值”或“获取或设置一个指示是否的值”。例如,考虑下面的布尔属性,它只公开get访问器:

/// <summary>
/// Gets a value indicating whether the item is enabled.
/// </summary>
public bool Enabled
{
    get { return this.enabled; }
}
//
///获取一个值,该值指示该项是否已启用。
/// 
公共布尔启用
{
获取{返回this.enabled;}
}
在某些情况下,属性的set访问器可以比get访问器具有更多受限的访问权限。例如:

/// <summary>
/// Gets the name of the customer. 
/// </summary>
public string Name
{
    get { return this.name; }
    private set { this.name = value; }
}
/// <summary>
/// Gets or sets the name of the customer. 
/// </summary>
protected string Name
{
    get { return this.name; }
    set { this.name = value; }
}
internal class Class1
{
    /// <summary>
    /// Gets or sets the name of the customer. 
    /// </summary>
    protected string Name
    {
        get { return this.name; }
        internal set { this.name = value; }
    }
}

internal class Class1
{
    public class Class2
    {
        /// <summary>
        /// Gets or sets the name of the customer. 
        /// </summary>
        public string Name
        {
            get { return this.name; }
            internal set { this.name = value; }
        }
    }
}
//
///获取客户的名称。
/// 
公共字符串名
{
获取{返回this.name;}
私有集{this.name=value;}
}
在本例中,集合访问器被授予了私有访问权限,这意味着它只能由包含它的类的本地成员访问。但是,get访问器从父属性继承其访问权限,因此任何调用方都可以访问它,因为该属性具有公共访问权限

在这种情况下,文档摘要文本应该避免引用set访问器,因为它对外部调用方不可见

StyleCop应用一系列规则来确定何时应该在属性的摘要文档中引用集合访问器。通常,这些规则要求只要set访问器对与get访问器相同的一组调用者可见,或者只要对外部类或继承类可见,就引用set访问器

确定是否在属性的摘要文档中包含集合访问器的具体规则如下:

1.set访问器与get访问器具有相同的访问级别。例如:

/// <summary>
/// Gets the name of the customer. 
/// </summary>
public string Name
{
    get { return this.name; }
    private set { this.name = value; }
}
/// <summary>
/// Gets or sets the name of the customer. 
/// </summary>
protected string Name
{
    get { return this.name; }
    set { this.name = value; }
}
internal class Class1
{
    /// <summary>
    /// Gets or sets the name of the customer. 
    /// </summary>
    protected string Name
    {
        get { return this.name; }
        internal set { this.name = value; }
    }
}

internal class Class1
{
    public class Class2
    {
        /// <summary>
        /// Gets or sets the name of the customer. 
        /// </summary>
        public string Name
        {
            get { return this.name; }
            internal set { this.name = value; }
        }
    }
}
//
///获取或设置客户的名称。
/// 
受保护字符串名
{
获取{返回this.name;}
设置{this.name=value;}
}
2.该属性只能在程序集中进行内部访问,并且set访问器也具有内部访问权限。例如:

/// <summary>
/// Gets the name of the customer. 
/// </summary>
public string Name
{
    get { return this.name; }
    private set { this.name = value; }
}
/// <summary>
/// Gets or sets the name of the customer. 
/// </summary>
protected string Name
{
    get { return this.name; }
    set { this.name = value; }
}
internal class Class1
{
    /// <summary>
    /// Gets or sets the name of the customer. 
    /// </summary>
    protected string Name
    {
        get { return this.name; }
        internal set { this.name = value; }
    }
}

internal class Class1
{
    public class Class2
    {
        /// <summary>
        /// Gets or sets the name of the customer. 
        /// </summary>
        public string Name
        {
            get { return this.name; }
            internal set { this.name = value; }
        }
    }
}
内部类Class1
{
/// 
///获取或设置客户的名称。
/// 
受保护字符串名
{
获取{返回this.name;}
内部集合{this.name=value;}
}
}
内部类1
{
公共课2
{
/// 
///获取或设置客户的名称。
/// 
公共字符串名
{
获取{返回this.name;}
内部集合{this.name=value;}
}
}
}
3.属性是private或包含在private类下,set访问器具有private以外的任何访问修饰符。在下面的示例中,集合访问器上声明的访问修饰符没有任何意义,因为集合访问器包含在私有类中,因此不能被Class1之外的其他类看到。这有效地为set访问器提供了与get访问器相同的访问级别

public class Class1
{
    private class Class2
    {
        public class Class3
        {
            /// <summary>
            /// Gets or sets the name of the customer. 
            /// </summary>
            public string Name
            {
                get { return this.name; }
                internal set { this.name = value; }
            }
        }
    }
}
公共类1
{
私家班2班
{
公共班级3
{
/// 
///获取或设置客户的名称。
/// 
公共字符串名
{
获取{返回this.name;}
内部集合{this.name=value;}
}
}
}
}
4.每当集合存取器具有受保护或受保护的内部存取时,应在文档中引用它。从包含属性的类继承的类始终可以看到受保护或受保护的内部集访问器

internal class Class1
{
    public class Class2
    {
        /// <summary>
        /// Gets or sets the name of the customer. 
        /// </summary>
        internal string Name
        {
            get { return this.name; }
            protected set { this.name = value; }
        }
    }

    private class Class3 : Class2
    {
        public Class3(string name) { this.Name = name; }
    }
}
内部类Class1
{
公共课2
{
/// 
///获取或设置客户的名称。
/// 
内部字符串名
{
获取{返回this.name;}
受保护集{this.name=value;}
}
}
私有类Class3:Class2
{
公共类3(字符串名){this.name=name;}
}
}

M$对此有一些建议:不过,它们只是建议——您可以在自定义生成器中使用您喜欢的任何东西。还有一些标记用于intelli-sense。可能希望将“XML”作为标记添加到您的问题中..Ghostdoc很好,但自动生成的注释并没有真正的帮助,可以忽略。是的,但它会自动添加正确的结构。之后,您始终可以编辑自动生成的文本。半吨