Internal abstract class: how to hide usage outside assembly?

后端 未结 7 1072
再見小時候
再見小時候 2020-12-05 01:30

I have a common assembly/project that has an abstract base class, then several derived classes that I want to make public to other assemblies.

I don\'t want the abs

7条回答
  •  我在风中等你
    2020-12-05 02:24

    As I understand, you want your abstract class to only be implemented by other classes in the same assembly (e.g. it is internal) but the derived classes could be public.

    The way to do this is to make the abstract base class public, but give it an internal default constructor:

    public abstract class MyClass
    {
        internal MyClass() { }
    }
    

    This will allow MyClass (and hence its members) to be visible and usable to classes outside your assembly, but classes outside your assembly cannot inherit from it (will get a compile error).

    Edit: If classes which can be seen by external assemblies inherit from MyClass, you cannot prevent MyClass from also being seen - e.g., showing up in Intellisense. However, you can prevent them from being used by following the above.

提交回复
热议问题