title: Call method of all classes implementing an interface
body: <p>Using C# 10 I have the interface:</p>
  <pre><code>public Interface IEndpoint {
     void Map();
  } 
  </code></pre>
  <p>I need to find classes that implement <code>IEndpoint</code> in Assembly of <code>Program</code> class.</p>
  <p>Then, for each class, I need to call the method <code>Map</code>:</p>
  <pre><code>IEnumerable&lt;IEndpoint&gt; endpoints = 
    typeof(Program).Assembly
      .GetTypes()
      .Where(x =&gt; x.GetInterface(nameof(IEndpoint)) == typeof(IEndpoint)); 
  </code></pre>
  <p>I am getting the error:</p>
  <pre><code>Cannot implicitly convert type 'System.Collections.Generic.IEnumerable&lt;System.Type&gt;' to 'System.Collections.Generic.IEnumerable&lt;IEndpoint&gt;'
  </code></pre>
  <p>How can I solve this and call the method <code>Map</code> for each found class?</p>
  <p><strong>Update</strong></p>
  <p>I think I got it:</p>
  <pre><code>IEnumerable&lt;Type&gt; types = typeof(Program).Assembly.GetTypes().Where(x =&gt; x.IsAssignableFrom(typeof(IEndpoint)) &amp;&amp; !x.IsInterface);
  
  foreach (Type type in types) {
    
    IEndpoint? endpoint = (IEndpoint?)Activator.CreateInstance(type);
  
    endpoint?.Map();
  
  }
  </code></pre>
  <p>Am I missing something in my implementation?</p>
  <p>I am asking because I've seen many approaches to this problem.</p>
  <p>Not sure if mine is the most recent approach.</p>