首先看一下这段代码:
class Program
{
static void Main(string[] args)
{
var x =(Base) new Derived();
((IMethod)x).DoWork();
Console.ReadKey();
}
}
interface IMethod
{
void DoWork();
}
abstract class Base : IMethod
{
void IMethod.DoWork()
{
Console.WriteLine("Base.DoWork");
}
}
class Derived : Base, IMethod
{
public void DoWork()
{
//here I where I want to call base.DoWork();
Console.WriteLine("Derived.DoWork");
}
}
输出:
Derived.DoWork
期望的结果:
Base.DoWork
Derived.DoWork
我正在处理一个API,它公开了一个接口,并在实现时,在某个进程的某个部分将调用DoWork
方法。
现在在上面的例子中,Base
类是API的一部分,在API内部已经显式地实现了该接口并在DoWork
方法中执行了一些重要操作。
我需要在我的派生类中覆盖IMethod
的实现,以便在需要时得到通知,但问题是我无法“覆盖”该方法并调用基方法,也无法将基类转换为IMethod
。
有解决方案吗?
注意:由于这是Silveright项目,因此反射将不起作用,而且私有方法调用被禁止。