I have a method in class library that scans all loaded assemblies for types that implement a certain interface a la Getting all types that implement an interface.
var type = typeof(IMyInteraface);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
When installing the class library into projects, I occasionally run into a FileNotFoundException
like:
Could not load file or assembly 'AAA.BBB.CCC, Version=1.2.3.4, Culture=neutral, PublicKeyToken=abcdef0123456789a' or one of its dependencies. The system cannot find the file specified.
I want to rewrite the LINQ query into something more safe from errors. For my purposes, if an assembly cant be loaded, then I don't have to worry about trying to load any type that belongs to it. How is this done?