程序集向外界公开了几个接口(IFirst
,ISecond
,IThird
),即那些接口为public
。
现在,作为一个实现细节,所有这些对象都有一个共同的特征,由接口IBase
描述。我不想让IBase
公开,因为这可能会在将来的实现中发生变化,并且与我的程序集的用户完全无关。
显然,公共接口不能从内部接口派生(给我一个编译器错误)。
有没有办法表达IFirst
,ISecond
和IThird
从内部角度来看有什么共同之处?
答案 0 :(得分:3)
不在C#
您可以做的最好的事情是让类实现内部和公共接口。
答案 1 :(得分:0)
正如安德鲁上面所说。只是为了扩展,这是一个代码示例:
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";
}
}