grouping collection of item by interface implementation
<p>I have a collection of object that all have a common base type. These object are derived at many different level and all properties come from interfaces implementation.</p>

<pre><code>// my list of all the items i have (Item being the base class)
var items = new List&lt;Item&gt;();
</code></pre>

<p>Now i need to group those item by interfaces they implement but each class implement multiple interfaces.</p>

<p>To get all my interfaces i use the Assembly reflection like so :</p>

<pre><code>Assembly.GetExecutingAssembly().GetTypes().Where(o =&gt; o.IsInterface).ToList();
</code></pre>

<p>This is very cute but the problem is that i get a list of <code>Type</code> object so i cannot easily iterate and use Linq to query the items for each type and retrieve the collection grouped. I know i have to build the collection manually i was hoping to use a <code>OfType&lt;&gt;</code> but i can't with a <code>Type</code>.</p>

<p>here what i was hoping it to look like in the final :</p>

<pre><code>// my list of all the items i have (Item being the base class)
var items = new List&lt;Item&gt;();

// get all interface of my assembly
var interfaces = Assembly.GetExecutingAssembly().GetTypes().Where(o =&gt; o.IsInterface).ToList();

// create an anonymous group with type and list of items of that type
var groups = interfaces.Select(interfaceType=&gt; 
    new 
        { 
            Type = interfaceType, 
            Items = items.OfType&lt;interfaceType&gt;().ToList() // here's my issue
        });
</code></pre>

<p>I am wondering if there is something i have overlooked and didn't see or do i really have to compare items by item to see if they implement the type. Or my second solution is using a <code>items.Where(item =&gt; items as [My Type]).Where(item=&gt; item != null)</code> which is not much cleaner.</p>

<p>Edit :
here's a sample of data that might help</p>

<pre><code>inputs (3 classes) : 

item 1 : ISquare, IRectangle, IData
item 2 : IRectangle, IData, IReporting
item 3 : IData

Desired output (collection of 4 because 4 interface exists) :

{ ISquare, { item 1 }},
{ IRectangle, { item 1, item 2 }},
{ IData, { item 1, item 2, item 3 }},
{ IReporting, { item 2, item 3 }}
</code></pre>