Inconsistent accessibility: base class is less accessible than class

穿精又带淫゛_ 提交于 2019-11-30 21:57:13

问题


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 of the dll.

How do I do that?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/13270414/inconsistent-accessibility-base-class-is-less-accessible-than-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!