Inconsistent accessibility: base class is less accessible than class

后端 未结 3 1004
梦谈多话
梦谈多话 2021-01-07 18:46

So I have an abstract base class in a DLL and child classes of that class. I want the childs to be public, but the base to be private so that it cannot be accessed outside o

相关标签:
3条回答
  • 2021-01-07 19:13

    Make it public, make all constructors internal (if you're using the default constructor, add a parameterless constructor to override that).

    Then while public and not sealed, it can't be sub-classed by external code.

    0 讨论(0)
  • 2021-01-07 19:23

    Just to clarify what I was saying in comments on @Marc Gravel's answer you could

    public ChildClass : ParentClass
    {
    
    }
    
    public ParentClass
    {
       internal void MethodIdontWantToExpose()
      {
    
      }
    
    }
    

    That said an interface is probably the best solution

    0 讨论(0)
  • 2021-01-07 19:28

    You don't and you can't.

    If you want to expose the class as public, the base-type must be public. One other option is to have a public interface, and only expose the type via the interface (presumably with a factory method somewhere for creating instances).

    One final option is to encapsulate the base-class rather than inherit it.

    0 讨论(0)
提交回复
热议问题