公共接口来自内部?

时间:2013-02-17 15:27:13

标签: c# inheritance interface public internal

程序集向外界公开了几个接口(IFirstISecondIThird),即那些接口为public

现在,作为一个实现细节,所有这些对象都有一个共同的特征,由接口IBase描述。我不想让IBase公开,因为这可能会在将来的实现中发生变化,并且与我的程序集的用户完全无关。

显然,公共接口不能从内部接口派生(给我一个编译器错误)。

有没有办法表达IFirstISecondIThird从内部角度来看有什么共同之处?

2 个答案:

答案 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";
    }
}