title: Is it possible to restrict a base class to a specific subclass?
body: <p>I have a need where I would like to have a base class only available to one specific subclass. This is due to a limitation where you cannot define an <code>Attribute</code> as an inner class of a generic.</p>
  <p>The reason we want to define the attribute within the generic is when you do that and set its type to <code>protected</code>, that attribute is now only available to subclasses of your base class, keeping your API clean.</p>
  <p>As an example, consider the abstract class <code>ExampleMarkupExtension&lt;T&gt;</code> that inherits from <code>MarkupExtension</code>.  I want to define the attribute <code>StaticInfoAttribute</code> as an inner class, so it is only available to subclasses of <code>ExampleMarkupExtension&lt;T&gt;</code>.</p>
  <p>However, as mentioned, you can't define an attribute as an inner class of a generic, so my workaround is to create a second, non-generic <code>ExampleMarkupExtensionBase</code> class which inherits from <code>MarkupExtension</code>, define <code>StaticInfoAttribute</code> as an inner class there, then have the generic inherit from that class instead.</p>
  <p>The issue is now I have an extra class in my hierarchy that anyone can subclass.  While it's technically harmless, I like keeping my API surface area clean.</p>
  <h1>Example</h1>
  <h3><code>ExampleMarkupExtensionBase</code> : Non-generic abstract base class</h3>
  <p>This is the class that defines the attribute which should only be available to subclasses of this class. This class only exists because I can't put <code>StaticInfoAttribute</code> within the generic abstract base class <code>ExampleMarkupExtension&lt;T&gt;</code> below.</p>
  <pre><code>public abstract class ExampleMarkupExtensionBase : MarkupExtension {
  
      [AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
      protected class StaticInfoAttribute : Attribute {
          public StaticInfoAttribute(string value) =&gt; Value = value;
          public readonly string Value;
      }
  }
  </code></pre>
  <h3><code>ExampleMarkupExtension&lt;T&gt;</code> : Generic abstract base class</h3>
  <p>This is the class that holds the static members which are unique to the subclasses of this class. It does this by using the subclass as the type parameter for the base class (see the <code>where</code> clause). The value of the static field is set from the attribute, hence it only being of any use for this specific subclass of <code>ExampleMarkupExtension</code> above.</p>
  <pre><code>public abstract class ExampleMarkupExtension&lt;T&gt; : ExampleMarkupExtensionBase
  where T : ExampleMarkupExtension&lt;T&gt; {
  
      static ExampleMarkupExtension() =&gt; Value = typeof(T).GetRequiredCustomAttribute&lt;StaticInfoAttribute&gt;().Value;
  
      public static readonly string Value;
  
      public sealed override object ProvideValue(IServiceProvider serviceProvider)
          =&gt; Value;
  }
  </code></pre>
  <h2>Rationale</h2>
  <p>Some may ask what's the harm of leaving it as-is.  Technically, there's nothing wrong with letting a person subclass <code>ExampleMarkupExtensionBase</code>. There's just no point to it.</p>
  <p>For that matter, there's technically no harm with moving the <code>StaticInfoAttribute</code> outside of the class as well, removing the need for the non-generic base class entirely. I just chose the former approach as it's more important to restrict the attribute's usage than it is to stop someone from subclassing the base class.</p>