[英]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. 派生类必须在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. 您可以调用一个通用方法,您可以在
base
类中重写该方法。
So in localReport_SubreportProcessing
, call ProcessSubreport
因此,在
localReport_SubreportProcessing
,调用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.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.