我最近研究了一些代码,遇到了一个派生接口,它声明new
与基本接口具有完全相同的名称和签名的方法:
public interface IBase
{
Result Process(Settings settings);
}
public interface IDerived : IBase
{
new Result Process(Settings settings);
}
我想知道这是否有原因。根据我的理解,我可以安全地删除后一个方法声明并离开IDerived
空,而不会破坏任何使用它的代码。我错了吗?
附:如果这很重要,那么这些接口声明还具有以下属性:ComVisible(true)
, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
and Guid(...)
.
好吧,你may能够摆脱它 - 但如果你摆脱了方法IDerived
,严格来说,实际上并不相同。一个实现实际上可以提供两种不同的方法来不同地实现两个接口方法。
例如:
using System;
public interface IBase
{
void Process();
}
public interface IDerived : IBase
{
new void Process();
}
public class FunkyImpl : IDerived
{
void IBase.Process()
{
Console.WriteLine("IBase.Process");
}
void IDerived.Process()
{
Console.WriteLine("IDerived.Process");
}
}
class Test
{
static void Main()
{
var funky = new FunkyImpl();
IBase b = funky;
IDerived d = funky;
b.Process();
d.Process();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)