是否可以从基类继承抽象方法体中的注释?

时间:2014-09-03 13:41:49

标签: c# inheritance comments resharper ghostdoc

我有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并且它的实现通常看起来每次都相同,所以它会很有用。

1 个答案:

答案 0 :(得分:4)

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

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

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 。它将从基类中提取注释。

执行上述步骤后,派生类看起来会像这样。

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