繁体   English   中英

在基类中调用访问器的基实现,获取子实现

[英]Calling base implementation of an accessor inside base class, getting child implementation

我试图在我们的代码库中设置一系列对象,以便能够使用 HTML 提供格式良好的字符串表示。 但是,当我尝试检索子类的通知文本时,当它获取父访问器时,它会检索子类的NotificationFormat实现,而不是基类NotificationFormat

尝试通过使用this来澄清没有区别,Resharper 建议删除多余的限定符。 如果我改为将子类更改为在我尝试保留的方法上使用new ,那么当我访问被视为基类的子类上的NotificationText时,我只会得到基类NotificationText 我曾认为没有办法从父级访问任何子级实现。

我的目标是能够在可能是基类或子类的对象上调用NotificationText 如果我在一个实际上是子类的对象上调用它,我想使用子实现并让它回调到基类格式信息的基实现。

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

public class Program
{   
    public static void Main()
    {
        var TestB = new B();
        TestB.Details = "Some Details";
        TestB.Name = "A Name";
        TestB.ID = 1;

        Console.WriteLine(TestB.NotificationText); // works

        var TestA = (A)TestB;
        Console.WriteLine(TestA.NotificationText); // only returns A notification Text
    }
}

public class A
{
    public long? ID {get;set;}

    public virtual string NotificationFormat
    {
        get
        {
            return "<h3>General Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Id:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }

    public virtual string NotificationText
    {
        get
        {
            return string.Format(NotificationFormat, ID);
        }
    }
}

public class B : A
{
    public string Name {get;set;}
    public string Details {get;set;}

    // chnage override to new to get it to compile
    public override string NotificationFormat
    {
        get
        {
            return "<h3>Specific Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Name:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "<tr>" + 
                   "<td>Details:</td>" + 
                   "<td>{1}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }   

    // chnage override to new to get it to compile
    public override string NotificationText
    {
        get
        {
            var baseNotification = base.NotificationText;
            return baseNotification + string.Format(NotificationFormat, Name, Details);
        }
    }
}

由于NotficationFormatvirtualNotification任何调用都将调用最派生的实现。 当你调用base.NotificationTextB ,在代码A.NotificationText使得一个虚拟呼叫NotificaitonFormat所以它会调用B.NotificationFormat 听起来您不希望NotificationFormat是虚拟的:

public class A
{
    public long? ID {get;set;}

    private string NotificationFormat
    {
        get
        {
            return "<h3>General Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Id:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }

    public virtual string NotificationText
    {
        get
        {
            return string.Format(NotificationFormat, ID);
        }
    }
}

public class B : A
{
    public string Name {get;set;}
    public string Details {get;set;}

    private string NotificationFormat
    {
        get
        {
            return "<h3>Specific Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Name:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "<tr>" + 
                   "<td>Details:</td>" + 
                   "<td>{1}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }   

    // change override to new to get it to compile
    public override string NotificationText
    {
        get
        {
            var baseNotification = base.NotificationText;
            return baseNotification + string.Format(NotificationFormat, Name, Details);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM