var type = typeof(TInterface);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(s => s.GetTypes())
.Where(t => type.IsAssignableFrom(t));
This code is going slower than I would like. Can someone suggest a more optimal way to code this in C#?
You are iterating over all types in all assemblies you have loaded/referenced. But the type you want is your type so you know it isn’t in any of the system assemblies. So for example you can filter out assemblies in the global assembly cache if you program isn’t installed there:
You can use other filtering strategies to restrict the assemblies to your own if your application is installed in the GAC.