Getting all types from core library that implements specific interface
Sometimes you don’t know which of all .NET framework types better fits you requirements, but you know that this class must implement some interface.
Following code gets all types that implements specific interface by interface name:
using System;
using System.Linq;
using System.Reflection;
class Program
{
static void Main(string[] args)
{
string interfaceName = "ICollection";
Type t = (new object()).GetType();
Assembly assembly = t.Assembly;
var types = from a in assembly.GetTypes()
where a.GetInterface(interfaceName) != null
select a;
foreach (Type type in types)
{
Console.WriteLine(type.ToString());
}
Console.ReadKey();
}
}
You can use other type from other assembly, for example, from SharePoint to search all types that has implemented necessary interface or load required assembly directly from DLL file.