【问题标题】:Is it possible to inherit comments in an abstract method's body from a BaseClass?是否可以从基类继承抽象方法体中的注释?
【发布时间】:2014-10-28 01:12:53
【问题描述】:

我有 PersonBaseClass 和派生自它的 EmployeeClass。 现在我想在 BaseClass 中定义一个方法体作为注释,当我使用 Resharper 的“实现成员”(或手动实现它们)时,它也会将它放在方法体中。

大概是这样的:

public abstract class PersonBaseClass : DependencyObject
{
   //<methodComment>
   // object connectionString;
   // _configurations.TryGetValue("ConnectionString", out connectionString);
   //</methodComment>
   protected abstract void InstanceOnPersonChanged(object sender, EventArgs eventArgs);
}

实施后将如下所示:

public class EmployeeClass : PersonBaseClass
{
    protected override void InstanceOnPersonChanged(object sender, EventArgs eventArgs)
    {
        // object connectionString;
        // _configurations.TryGetValue("ConnectionString", out connectionString);
    }
}

这已经可能吗?无法使其与 GhostDoc 一起使用。

编辑:这会很有用,因为我希望将 BaseClass 放在库中,并且它的实现通常每次看起来都一样。

【问题讨论】:

  • Ghost 文档足以将基类成员的评论拉到覆盖成员的评论中。您似乎要求在方法中嵌入评论。由于您的要求很奇怪,因此此功能不太可能可用。
  • @Sriram Sakthivel:我该怎么做?我还在上面编辑了我的问题,以解释我将其用于什么。
  • 如果“它的实现通常每次看起来都一样”,为什么不把它变成一个虚拟方法并且只改变它(即覆盖它),当它真的不同时呢? -- 或者拆分方法,使得只有真正不同的部分在抽象方法中。

标签: c# inheritance comments resharper ghostdoc


【解决方案1】:

不完全是您所要求的,但您可以使用 Ghostdoc 将 cmets 拉到继承的成员。请注意,它不会将 cmets 添加到派生类方法的主体中,但会将其添加到其注释部分。

假设你有一个这样的类,带有注释嘿,这是一个很棒的方法:)

public abstract class PersonBaseClass
{
    /// <summary>
    /// Hey, this is great method :)
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="eventArgs">The <see cref="EventArgs" /> instance containing the event data.</param>
    protected abstract void InstanceOnPersonChanged(object sender, EventArgs eventArgs);
}

然后你添加派生类并像这样覆盖成员

public class EmployeeClass : PersonBaseClass
{
    protected override void InstanceOnPersonChanged(object sender, EventArgs eventArgs)
    {
        throw new NotImplementedException();
    }
}

然后将光标放在派生类成员的主体内并按ctrl + shift + d。它将从基类中提取 cmets。

执行上述步骤后,您的派生类将如下所示。

public class EmployeeClass : PersonBaseClass
{
    /// <summary>
    /// Hey, this is great method :)
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="eventArgs">The <see cref="EventArgs" /> instance containing the event data.</param>
    /// <exception cref="System.NotImplementedException"></exception>
    protected override void InstanceOnPersonChanged(object sender, EventArgs eventArgs)
    {
        throw new NotImplementedException();
    }
}

【讨论】:

  • 谢谢!这正是我所需要的。注释是否在方法体内并不重要。
  • 是否有可能以某种方式表明一个方法有这种附加的注释,或者是按 ctrl + shift + d 找出唯一的方法?
  • @mYnDstrEAm AFAIK 没有。寻找它,我不知道这种事情。
猜你喜欢
  • 2014-02-19
  • 1970-01-01
  • 2013-07-01
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
相关资源
最近更新 更多