【问题标题】:how are these XML comments generated in C# code behind automatically?这些 XML 注释是如何在 C# 代码中自动生成的?
【发布时间】:2015-08-27 12:31:20
【问题描述】:

当我查看代码隐藏时,我看到每个属性和类名都有很多 /// xml cmets,但是这些 cmets 是如何生成的?

自动?通过第三方?我不认为我的前同事为每个属性和类都输入///....

生成的自动方式(工具/快捷键/其他)是什么?

public interface IRepository<TEntity> where TEntity : class
{
    /// <summary>
    /// Gets the list of items from the repository
    /// </summary>
    /// <returns></returns>
    IEnumerable<TEntity> Get();

    /// <summary>
    /// Gets the single entity from the repository
    /// </summary>
    /// <param name="id">Id of the entity</param>
    /// <returns></returns>
    TEntity Get(Int32 id);

    /// <summary>
    /// Adds an entity to the repository
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    TEntity Add(TEntity entity);

    /// <summary>
    /// Updates an entity in the repository
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    Int32 Update(TEntity entity);
}

【问题讨论】:

  • 听起来你最好的选择是问问你的前同事。也许是 GhostDoc?
  • 它在我看来确实像 GhostDoc。它确实尝试自己计算出 cmets,但屡屡失败。我让 ghost doc 生成 cmets,然后如果它们需要扩展或没有意义,我自己修改它们。

标签: c# .net visual-studio


【解决方案1】:

GhostDoc 将帮助您自动生成 XmlComment。

如果您想手动操作,那么当您在任何属性、方法、构造函数、类或接口上方键入 / 3 次 (///) 时,将自动生成相关注释,其中包含空摘要、参数和返回值,您必须编写您自己的关于您的实现的定义/描述。这些 cmets 用于 Intellisense

工作原理:

如果我有以下带有 XML 注释的构造函数:

/// <summary>
/// The constructor sets the name, age and cash
/// </summary>
/// <param name="name">The name of the guy</param>
/// <param name="cash">The amount of cash the guy starts with</param>
public Guy(string name, int age, int cash) {
     //This is constructor implementation with XML Comments
}

然后当您初始化或使用此构造函数时,智能感知将显示此 XML 注释,如下图所示

【讨论】:

    【解决方案2】:

    如果您在 Visual Studio 中任何属性/类/方法声明上方的行中键入三次/,它将生成这些 sn-ps。

    智能感知和其他第三方工具使用它们为项目生成文档。

    【讨论】:

      【解决方案3】:

      你可以使用GhostDoc,它可以为你自动生成xml cmets。

      GhostDoc 是一个自动生成 XML 的 Visual Studio 扩展 基于类型的方法和属性的文档 cmets, 参数、名称和其他上下文信息。

      【讨论】:

        【解决方案4】:

        当您在 Visual Studio 中的方法/类/属性上方输入 /// 时,它将自动生成一个结构,其中包括参数等项目以及该项目的返回值(如果适用)。这在这篇 MSDN 文章 here 中有更详细的说明。

        【讨论】:

          猜你喜欢
          • 2012-10-27
          • 1970-01-01
          • 1970-01-01
          • 2011-01-18
          • 1970-01-01
          • 2012-02-09
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          相关资源
          最近更新 更多