简体   繁体   中英

Inherit event handlers

Im trying to write abstract class for different reports.

I have a method

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

which has such lines

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

Derived class must write own code in the localReport_SubreportProcessing.

I'm not sure how to make inheritance here. Can someone help ?

Rather than having a method:

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

consider instead:

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

Now your subclasses can simply use:

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

You can call a common method, which you override in your base class.

So in localReport_SubreportProcessing , call ProcessSubreport

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

protected virtual void ProcessSubreport()
{ }

And override it in your deriving class:

protected override void ProcessSubreport()
{ }

Try like below.

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.
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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