【问题标题】:Public interface deriving from internal?来自内部的公共接口?
【发布时间】:2013-02-02 01:55:01
【问题描述】:
一个程序集向外界公开几个接口(@987654321@、ISecond、IThird),即这些接口是public。
现在,作为一个实现细节,所有这些对象都有一个共同的特征,由接口IBase 描述。我不想公开IBase,因为这可能会在未来的实现中发生变化,并且与我的程序集的用户完全无关。
显然,公共接口不能从内部接口派生(给我一个编译器错误)。
有什么方法可以表达IFirst、ISecond 和IThird 仅从内部角度来看有共同点?
【问题讨论】:
标签:
c#
inheritance
interface
public
internal
【解决方案1】:
不在 C# 中
你能做的最好的就是让你的类实现内部和公共接口。
【解决方案2】:
正如安德鲁上面所说。只是为了扩展,这里有一个代码示例:
public interface IFirst
{
string FirstMethod();
}
public interface ISecond
{
string SecondMethod();
}
internal interface IBase
{
string BaseMethod();
}
public class First: IFirst, IBase
{
public static IFirst Create() // Don't really need a factory method;
{ // this is just as an example.
return new First();
}
private First() // Don't really need to make this private,
{ // I'm just doing this as an example.
}
public string FirstMethod()
{
return "FirstMethod";
}
public string BaseMethod()
{
return "BaseMethod";
}
}
public class Second: ISecond, IBase
{
public static ISecond Create() // Don't really need a factory method;
{ // this is just as an example.
return new Second();
}
private Second() // Don't really need to make this private,
{ // I'm just doing this as an example.
}
public string SecondMethod()
{
return "SecondMethod";
}
public string BaseMethod()
{
return "BaseMethod";
}
}