C# - 如何从隐藏方法(新关键字)继承文档

发布于 2025-02-08 16:50:14 字数 475 浏览 1 评论 0原文

有没有办法不隐藏儿童类中原始方法的文档?例如;

    public class Parent
    {
        /// <summary>
        /// Boo!!!
        /// </summary>
        public void Foo()
        {
            // ... code here
        }
    }

    public class Child : Parent
    {
        // inheritdoc doesn't work here

        /// <inheritdoc/>
        public new void Foo()
        {
            base.Foo();
            // ... additional logic here
        }
    }

Is there a way not to hide the documentation of the original method in the child class? For example;

    public class Parent
    {
        /// <summary>
        /// Boo!!!
        /// </summary>
        public void Foo()
        {
            // ... code here
        }
    }

    public class Child : Parent
    {
        // inheritdoc doesn't work here

        /// <inheritdoc/>
        public new void Foo()
        {
            base.Foo();
            // ... additional logic here
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2025-02-15 16:50:14

正如穆罕默德(Muhammad)刚刚说的那样:通过宣布使用new关键字的方法,您基本上创建了一个新方法签名,该签名以任何方式与旧方法无关。 虚拟方法但是覆盖旧方法而不是介绍。因此,覆盖方法将继承其文档。

public class Parent
{

    /// <summary>
    ///  a classic method documentation
    /// </summary>
    public void Test1()
    {

    }

    /// <summary>
    /// a virtual method documentation
    /// </summary>
    public virtual void Test2()
    {

    }
}


public class Child : Parent
{
    /// <inheritdoc/>
    // does not inherit documentation
    public new void Test1()
    {
        base.Test1();
    }

    /// <inheritdoc/>
    // does inherit documentation
    public override void Test2()
    {
        base.Test2();
    }
}

As Muhammad just said: By declaring a method with the new keyword, you basically create a new method signature, that is not related to the old method in any way. A virtual method however overrides the old method instead of covering it up. Therefore overriding a method will inherit its documentation.

public class Parent
{

    /// <summary>
    ///  a classic method documentation
    /// </summary>
    public void Test1()
    {

    }

    /// <summary>
    /// a virtual method documentation
    /// </summary>
    public virtual void Test2()
    {

    }
}


public class Child : Parent
{
    /// <inheritdoc/>
    // does not inherit documentation
    public new void Test1()
    {
        base.Test1();
    }

    /// <inheritdoc/>
    // does inherit documentation
    public override void Test2()
    {
        base.Test2();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文