c#注释技术/注释的重用



我通常将类中的字段声明为私有字段以及从外部访问该字段的公共属性(到目前为止没有什么特别的smile):

private bool doILookGood;
public bool DoILookGood
{
   get { return doILookGood; }
   set { doILookGood = value; }
}

现在我想知道是否有一种优雅而有效的方法来注释这种情况,而不用两次编写相同的注释。换句话说,我想保留的功能,IDE显示给我一个变量注释,而鼠标悬停与工具提示。

到目前为止,我是这样评论的:

/// <summary>
/// This i always true.
/// </summary>
private bool doILookGood;
/// <summary>
/// This i always true.
/// </summary>
public bool DoILookGood
{
   get { return doILookGood; }
   set { doILookGood = value; }
}

,我想有这样的东西:

/// <summary>
/// This i always true.
/// </summary>
private bool doILookGood;
/// <summary cref="doILookGood" />
public bool DoILookGood
{
   get { return doILookGood; }
   set { doILookGood = value; }
}

我知道使用XML标记来评论私有字段不是很有意义,因为它们不会出现在生成的文档中,但我只想有(ide内部)注释工具提示。

也许有人有线索:)

尽量使用auto-properties。这将避免在不需要时使用私有成员。

public bool DoILookGood { get; set; }

如果不可能(例如在实现INotifyPropertyChanged时),下面是我如何处理它(请注意,这只是为了示例,我肯定会使用自动属性而不是下面的代码):

    /// <summary>
    /// Private member for <see cref="MyValue"/>.
    /// </summary>
    private bool myValue;
    /// <summary>
    /// Gets or sets a value indicating whether ...
    /// </summary>
    /// <value>
    ///   <c>true</c> if ...; otherwise, <c>false</c>.
    /// </value>
    public bool MyValue
    {
        get { return this.myValue; }
        set { this.myValue = value; }
    }

EDIT:我还建议使用GhostDoc来节省时间(一个能够自动生成注释的插件)

最新更新