I have class that will load all assemblies in a directory and then get all the types an see if they implement an interface. I cannot get the type comparison to work. In the
OK - this gave me the answer: invalidcastexception-when-using-assembly-loadfile
Here is the updated class:
public class PluginLoader
{
public static T[] GetInterfaceImplementor(string directory)
{
if (String.IsNullOrEmpty(directory)) { return null; } //sanity check
DirectoryInfo info = new DirectoryInfo(directory);
if (!info.Exists) { return null; } //make sure directory exists
var implementors = new List();
foreach (FileInfo file in info.GetFiles("*.dll")) //loop through all dll files in directory
{
Assembly currentAssembly = null;
try
{
var name = AssemblyName.GetAssemblyName(file.FullName);
currentAssembly = Assembly.Load(name);
}
catch (Exception ex)
{
continue;
}
currentAssembly.GetTypes()
.Where(t => t != typeof(T) && typeof(T).IsAssignableFrom(t))
.ToList()
.ForEach(x => implementors.Add((T)Activator.CreateInstance(x)));
}
return implementors.ToArray();
}
}