簡體   English   中英

繼承事件處理程序

[英]Inherit event handlers

我試圖為不同的報告編寫抽象類。

我有辦法

protected Tuple<byte[], string, string> RenderReport()

有這樣的台詞

var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;

派生類必須在localReport_SubreportProcessing中編寫自己的代碼。

我不確定如何在這里繼承。 有人可以幫忙嗎?

而不是擁有一種方法:

private void localReport_SubreportProcessing(...) {...}

考慮改為:

protected virtual void OnSubreportProcessing(...) {...}

現在,您的子類可以簡單地使用:

protected override void OnSubreportProcessing(...) {...}

您可以調用一個通用方法,您可以在base類中重寫該方法。

因此,在localReport_SubreportProcessing ,調用ProcessSubreport

private void localReport_SubreportProcessing(object sender, EventArgs e)
{
    this.ProcessSubreport();
}

protected virtual void ProcessSubreport()
{ }

並在派生類中覆蓋它:

protected override void ProcessSubreport()
{ }

嘗試如下。

public abstract class BaseReport
{
  ......
  protected Tuple<byte[], string, string> RenderReport()
  {
    var localReport = new LocalReport { ReportPath = _reportLocalFullName };
    ...
    localReport.SubreportProcessing += localReport_SubreportProcessing;
    ...
  }

  protected abstract void LocalReport_SubreportProcessing(object sender, EventArgs e);
}

public class DerivedReport1 : BaseReport
{
  protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
  {
    // Report generation logic for report1.
  }
}

public class DerivedReport2 : BaseReport
{
  protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
  {
    // Report generation logic for report2.
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM