title: List of Blazor components that implement an interface
body: <p>I am building a reusable Razor class library for Blazor components.</p>
  
  <p>I have an interface that any Blazor component could implement:</p>
  
  <pre><code>public interface IControllableComponent
  {
      void SetSomeValue(int someValue);
  }
  </code></pre>
  
  <p>How can I get a List of all components that implement that interface?</p>
  
  <pre><code>public interface IControllableComponentManager
  {
      void SetSomeValueForAllControllableComponents(int someValue);
  }
  
  public class ControllableComponentManager : IControllableComponentManager
  {
      IList&lt;IControllableComponent&gt; _controllableComponentList;
  
      public ControllableComponentManager()
      {
          _controllableComponentList = ??? // how to populate this list?
      }
  
      public void SetSomeValueForAllControllableComponents(int someValue)
      {
          foreach (var controllableComponent in _controllableComponentList)
          {
              controllableComponent.SetSomeValue(someValue);
          }
      }
  }
  </code></pre>
  
  <p>I want for my reusable Razor class library to be used in the code behind in different projects:</p>
  
  <pre><code>public class MyControllableComponentBase : ComponentBase, IControllableComponent
  {
      protected int _someValue;
  
      public void SetSomeValue(int someValue)
      {
          _someValue = someValue;
      }
  }
  </code></pre>
  
  <p>Is there a way to populate _controllableComponentList in ControllableComponentManager?</p>
  
  <p>I would like to do this in a clean, proper way, without using reflection. Preferably in the style of ASP.NET Core with dependency injection.</p>
  
  <p>The problem is that Blazor components are instantiated in Razor markup as <code>&lt;Component&gt;&lt;/Component&gt;</code> so I don't know how to get their references to add them to the <code>List&lt;&gt;</code>.</p>