我最近遇到了这个难题:假设我想使用两个库,每个库都为消费者和消费品定义了一个接口(interface):
#region Library A
// This region represents a library (A) defining some interfaces
/// <summary>
/// Interface to be implemented by any processor of type A
/// </summary>
interface AProcessor
{
void Process(AProcessable a);
}
/// <summary>
/// Interface to be implemented by any object processable by a processor of type A
/// </summary>
interface AProcessable
{
int MyPropertyA { get; set; }
}
#endregion
#region Library B
// This region represents a library (B) defining some other interfaces
/// <summary>
/// Interface to be implemented by any processor of type B
/// </summary>
interface BProcessor
{
void Process(BProcessable a);
}
/// <summary>
/// Interface to be implemented by any object processable by a processor of type B
/// </summary>
interface BProcessable
{
int MyPropertyB { get; set; }
}
#endregion
然后,在我的应用程序中,我实现了一个可使用类型,它实现了每个库中的两个可使用接口(interface):
/// <summary>
/// A type processable by processors of type A and processors of type B.
/// </summary>
class Processable :
AProcessable,
BProcessable
{
// Implements interface AProcessable
public int MyPropertyA { get; set; }
// Implements interface BProcessable
public int MyPropertyB { get; set; }
}
我实现了一个能够消费 Processable 类型对象的消费者类型,它实现了 AProcessable 和 BProcessable:
/// <summary>
/// A processor that implements an A-type processor and a B-type processor.
/// </summary>
class Processor :
AProcessor,
BProcessor
{
// I thought that Process method would implement AProcessor.Process() and
// BProcessor.Process() as it expects a Processable object as parameter which
// implements AProcessable and BProcessable, but it doesn't.
// Compiler rises an error telling that Processor doesn't implement neither
// AProcessor.Process() nor BProcessor.Process()
/*
public void Process(Processable a)
{
throw new NotImplementedException();
}
*/
// Implementing both methods does not work because it creates ambiguity: when
// we call Processor.Process(p) being "p" a Processable object the compiler
// doesn't know if it has to call Processor.Process(AProcessable) or
// Processor.Process(BProcessable).
/*
public void Process(AProcessable a)
{
throw new NotImplementedException();
}
public void Process(BProcessable a)
{
throw new NotImplementedException();
}
*/
}
¿我的设计错误是什么?正确的方法是什么?
提前致谢。
最佳答案
您正在寻找显式接口(interface)实现。此链接有一个教程:
http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx
基本上,方法签名显示接口(interface)类型:
void BProcessor.Process(BProcessable b)
{
}
并且只能通过直接引用 BProcessor
来访问:
BProcessor b;
b.Process(null);
对 BProcessor
的推断引用将不适用于显式实现:
Processor p;
p.Process(new BProcessable()); // This won't compile.
这允许您实现具有冲突成员名称的接口(interface)。如果它们的类型名称存在冲突,则您需要开始调查命名空间冲突。
您会注意到一个显式实现,因为当您为 Processor
获取 Intellisense 时,它将丢失 Process(BProcessable b)
。
如果您处理流程的代码恰好被共享,您可以执行以下操作:
public void Process(Processable p)
{
}
void AProcess.Process(AProcessable a)
{
Process((Processable)a);
}
void BProcess.Process(BProcessable b)
{
Process((Processable)b);
}
但是,我个人会避免这种类型的前向转换,因为不能保证(除非您完全控制代码)获得 Processable
类型。
关于c# - C#接口(interface)的设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843392/