我建议稍微更改您的基类,以便DoSomething
调用受保护的方法:
class A: ISomethingDoer
{
void ISomethingDoer.DoSomething()
{
DoSomethingImpl();
}
protected void DoSomethingImpl()
{
Something.Do();
}
}
然后B
你可以打电话DoSomethingImpl
:
class B: A, ISomethingDoer
{
void ISomethingDoer.DoSomething()
{
if (reason)
{
DoSomethingImpl(); //this does compile
}
SomethingElse.Do();
}
}
Lasse V. Karlsen建议的替代方法是使用反射:
class B: A, ISomethingDoer
{
void ISomethingDoer.DoSomething()
{
if (reason)
{
string baseName = $"{typeof(ISomethingDoer).FullName}.{nameof(DoSomething)}";
MethodInfo baseMethod = this.GetType().BaseType
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(m => m.IsPrivate && m.IsFinal && m.Name == baseName);
baseMethod.Invoke(this, new object[0]);
}
SomethingElse.Do();
}
}
但我不喜欢这种方法,因为它使用反射并且会变慢。我用这个答案来帮助我构建反射解决方案。
如果需要过滤方法的不同重载,可以使用GetParameters(),并且可以通过构建object[]
包含相同位置顺序的数组来指定参数。