【问题标题】:Public interface deriving from internal?来自内部的公共接口?
【发布时间】:2013-02-02 01:55:01
【问题描述】:

一个程序集向外界公开几个​​接口(@98​​7654321@、ISecondIThird),即这些接口是public

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

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

有什么方法可以表达IFirstISecondIThird 仅从内部角度来看有共同点?

【问题讨论】:

    标签: 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";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 2011-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-17
        相关资源
        最近更新 更多