注释C#的继承(实际上是任何语言)

jum*_*kie 84 c# inheritance comments

假设我有这个界面

public interface IFoo
{
    ///<summary>
    /// Foo method
    ///</summary>
    void Foo();

    ///<summary>
    /// Bar method
    ///</summary>
    void Bar();

    ///<summary>
    /// Situation normal
    ///</summary>
    void Snafu();
}
Run Code Online (Sandbox Code Playgroud)

而这堂课

public class Foo : IFoo
{
    public void Foo() { ... }
    public void Bar() { ... }
    public void Snafu() { ... }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法,还是有一个工具可以让我自动放入基类或接口中每个成员的注释?

因为我讨厌为每个派生的子类重写相同的注释!

Vad*_*dim 139

你总是可以使用<inheritdoc />标签.

public class Foo : IFoo
{
    /// <inheritdoc />
    public void Foo() { ... }
    /// <inheritdoc />
    public void Bar() { ... }
    /// <inheritdoc />
    public void Snafu() { ... }
}
Run Code Online (Sandbox Code Playgroud)

  • @gerleim看看Jeff Heaton一年前的回答,以及下面的评论.Sandcastle有<inheritdoc />,而不是C#. (12认同)
  • Resharper 2017.2改进了对inheritdoc的支持https://www.jetbrains.com/resharper/whatsnew/ (9认同)
  • 我不知道<inheritdoc />甚至存在......但据我所知,这种方法的评论没有出现intellisense. (5认同)
  • 我看到intellisense中的接口与inheritdoc的注释,以及派生类中根本没有code-doc.但那可能是因为我有重新整理器. (4认同)
  • Visual Studio 版本 16.4.0 及更高版本为此表示法提供智能感知!https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#net-productivity-164GA (4认同)
  • Visual Studio Enterprise 2017(版本15.9.3)不会为我显示继承的注释. (2认同)

Den*_*nis 19

/// <inheritdoc/>如果要继承,请使用.避免GhostDoc或类似的东西.

我同意,评论不是继承的,这很烦人.如果有人有时间(我希望我这样做),这将是一个相当简单的插件.

也就是说,在我们的代码库中,我们只在接口上放置XML注释,并为类添加额外的实现注释.这适用于我们,因为我们的类是私有/内部的,只有接口是公共的.每当我们通过接口使用对象时,我们都会在intellisence中显示完整的注释.

GhostDoc是一个良好的开端,并使该过程更容易编写注释.添加/删除参数,重新运行GhostDoc并更新描述时,使注释保持最新非常有用.


Jam*_*ran 16

GhostDoc正是如此.对于未继承的方法,它尝试从名称中创建描述.

FlingThing()"Flings the Thing"

  • 自动生成的文档对我来说似乎是一个非常糟糕的主意.它们不会添加任何有用的信息,只会不必要地炸掉代码.如果一个工具可以从其名称中理解方法的作用,那么一个人也可以理解并且不需要文档. (166认同)
  • @Lensflare虽然我100%同意你,只要依靠AGD _as is_,我应该指出AGD并不意味着像那样"做所有"魔术按钮.相反,它们应该用作模板生成器,以减少样板量,重复文档,你必须自己编写,这样你就可以专注于重要的东西.---例如,它可以为你生成`<summary>`,`<param>`,`<returns>`,`<throws>`,`etc ...`部分.很多次都有足够好的结果; 其他时候需要更正或扩大,但仍然减少整体努力. (13认同)
  • @Lensflare这是真的.我曾经不得不使用一个只有这样生成的注释的框架,它向方法/类添加了无信息.而不是"这个方法做这个和那个"的评论,如"这是Z类的方法XY".xD此外,你不能浏览代码,所以它进行了试验和错误.再也不!:-) (7认同)
  • 人们的文档不适合开发人员,而是建筑师,所以他们的屁股都被覆盖了:"嘿,我们可以阅读你项目的代码文档吗?当然,现在就是这样." (4认同)
  • GhostDoc非常棒,我不知道自己需要做的事情之一,但是现在离不开:o) (2认同)

Jef*_*ton 14

Java有这个,我一直都在使用它.做就是了:

/**
 * {@inheritDoc}
 */
Run Code Online (Sandbox Code Playgroud)

Javadoc工具可以解决这个问题.

C#有类似的标记:

<inheritDoc/>
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读更多:

http://www.ewoodruff.us/shfbdocs/html/79897974-ffc9-4b84-91a5-e50c66a0221d.htm

  • C#没有`<inheritdoc />`标记:**Sandcastle**有它.http://shfb.codeplex.com/ (37认同)
  • 有一个用户语音功能请求将<inheritdoc />添加到C#.在https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3745102-add-intellisense-support-for-the-inheritdoc-ta上投票 (8认同)
  • 当声明Java或C#时,它通常表示相关工具的社区.从字面意义上讲,Java和C#都没有太多的能力.声明Java或C#缺乏连接数据库的能力是一个学术论据,因为运行时库可以做到这一点. (4认同)
  • Visual Studio 版本 16.4.0 及更高版本为 &lt;inheritDoc/&gt; 提供智能感知!https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#net-productivity-164GA (3认同)
  • C# 和 Java(或任何其他编程语言)都没有任何“XML doc”元素。这些是**评论**。编译器对它们一无所知。它们都被第三方文档生成器严格使用,无论是 javadoc 还是 sandcastle 等等。 (2认同)

svi*_*ick 8

Resharper可以选择从基类或接口复制注释.

  • @Jazimov当您Alt + Enter覆盖方法时,有一个选项“从基础复制文档”。 (2认同)

gat*_*sby 7

我会说要直接使用

/// <inheritdoc cref="YourClass.YourMethod"/>  --> For methods inheritance
Run Code Online (Sandbox Code Playgroud)

/// <inheritdoc cref="YourClass"/>  --> For directly class inheritance
Run Code Online (Sandbox Code Playgroud)

您必须将此注释放在类/方法的上一行

这将从您已记录的界面获取您的注释信息,例如:

    /// <summary>
    /// This method is awesome!
    /// </summary>
    /// <param name="awesomeParam">The awesome parameter of the month!.</param>
    /// <returns>A <see cref="AwesomeObject"/> that is also awesome...</returns>
    AwesomeObject CreateAwesome(WhateverObject awesomeParam);
Run Code Online (Sandbox Code Playgroud)


小智 5

另一种方法是使用<see />XML文档标签。这是一些额外的工作,但可以立即使用...

这里有些例子:

/// <summary>
/// Implementation of <see cref="IFoo"/>.
/// </summary>
public class Foo : IFoo
{
    /// <summary>
    /// See <see cref="IFoo"/>.
    /// </summary>
    public void Foo() { ... }

    /// <summary>
    /// See <see cref="IFoo.Bar"/>
    /// </summary>
    public void Bar() { ... }

    /// <summary>
    /// This implementation of <see cref="IFoo.Snafu"/> uses the a caching algorithm for performance optimization.
    /// </summary>
    public void Snafu() { ... }
}
Run Code Online (Sandbox Code Playgroud)

更新:

我现在更喜欢使用/// <inheritdoc/>ReSharper支持的功能。