Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>var searchTarget = typeof(ITarget&lt;&gt;); var dict = Assembly.GetExecutingAssembly() .GetTypes() .SelectMany(t =&gt; t.GetInterfaces() .Where(i =&gt; i.IsGenericType &amp;&amp; (i.GetGenericTypeDefinition() == searchTarget) &amp;&amp; !i.ContainsGenericParameters), (t, i) =&gt; new { Key = i.GetGenericArguments()[0], Value = t }) .ToDictionary(x =&gt; x.Key, x =&gt; x.Value); </code></pre> <p>Note that if you have multiple classes implementing <code>ITarget&lt;&gt;</code> and using the same generic type argument -- for example, <code>class Foo : ITarget&lt;string&gt;</code> and <code>class Bar : ITarget&lt;string&gt;</code> -- then the <code>ToDictionary</code> call will fail with an <code>ArgumentException</code> complaining that you can't add the same key twice.</p> <p>If you do need a one-to-many mapping then you have a couple of options available.</p> <ol> <li><p>Use <a href="http://msdn.microsoft.com/en-us/library/bb549211.aspx" rel="nofollow noreferrer"><code>ToLookup</code></a> rather than <code>ToDictionary</code> to generate a <a href="http://msdn.microsoft.com/en-us/library/bb460184.aspx" rel="nofollow noreferrer"><code>Lookup&lt;K,V&gt;</code></a>:</p> <pre><code>var dict = Assembly.GetExecutingAssembly() .GetTypes() .SelectMany(/* ... */) .ToLookup(x =&gt; x.Key, x =&gt; x.Value); </code></pre></li> <li><p>If you prefer to work with something like a <code>Dictionary&lt;K,List&lt;V&gt;&gt;</code> then you could do this:</p> <pre><code>var dict = Assembly.GetExecutingAssembly() .GetTypes() .SelectMany(/* ... */) .GroupBy(x =&gt; x.Key, x =&gt; x.Value) .ToDictionary(g =&gt; g.Key, g =&gt; g.ToList()); </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload