C#注释技术/注释重用

标签 c# .net visual-studio coding-style xml-documentation

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

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 标记来评论私有(private)字段不是很有意义,因为它们不会出现在生成的文档中,但我再次只想拥有(IDE 内部)评论工具提示。

也许有人知道:)

最佳答案

尽可能使用自动属性。这将避免在不需要时使用私有(private)成员。

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; }
    }

编辑:我还建议使用 GhostDoc节省时间(一个能够自动生成评论的插件)。

关于C#注释技术/注释重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8636062/

相关文章:

c# - Listview Itemediting 不工作

c# - 比较从 double 转换的小数是否安全

c# - 加载解决方案时自动停止打开文件

c# - (JSON.NET) RuntimeBinderException 访问动态 JObject(在一台 PC 上工作,在另一台上失败)

sql - SSMS 2012 中的 VS 样式片段。它们存在吗?

.net - VS 2008 中的重构工具?

c# - 将 SQL Server 与 Dart 一起使用

c# - 如何使用 C# 和 FluentNHibernate 在 NHibernate 中获取脏实体

c# - 自定义控件的基类

c# - 如何在 app.config 文件中声明一个变量?