This is what I have so far. Is there a robust way to do this below
private static void LoadPlugins(IList
{
DirectoryInfo dInfo = new DirectoryInfo(GetExtensionsDirectory());
FileInfo[] files = dInfo.GetFiles("*.dll");
if (files != null)
{
foreach (FileInfo file in files)
{
string[] fileArray = file.Name.Split('.');
if (fileArray != null)
assemblies.Add(Assembly.Load(fileArray[0]));
}
IList
foreach (var assembly in copyAssemblies.ToList())
{
foreach (var assemblyType in assembly.GetExportedTypes())
{
int index = assembly.GetExportedTypes().ToList().IndexOf(assemblyType);
var implementsInterface = typeof(IPluginContract).IsAssignableFrom(assemblyType) && assemblyType.IsClass;
//If class assembly does not have IPluginContract, remove assembly from list
if (!implementsInterface && !assemblyType.IsInterface)
{
assemblies.RemoveAt(index);
}
}
}
}
}
David SmithPosted Nov 3, 2016, 9:37 AM
Basically I am trying to prevent loading dlls that is not expected to be loaded in memory because I will get exception on the on My MEF compose parts if someone make an mistake an copy an Dll into the Extesnion Directory. My compose part function blow up. So I am trying put in exception handling for bad Dlls and keep loading the assemblies that's need to be loaded. What is someone make an mistake copy a c++ or c sharp dll that does not even apart of my project. What do I do?
So what will be the appropriate logic to doing this below to what I am trying to accomplish?
private static void LoadPlugins(IList{
DirectoryInfo dInfo = new DirectoryInfo(GetExtensionsDirectory());
FileInfo[] files = dInfo.GetFiles("*.dll");
if (files != null)
{
foreach (FileInfo file in files)
{
string[] fileArray = file.Name.Split('.');
if (fileArray != null)
assemblies.Add(Assembly.Load(fileArray[0]));
}
IList
foreach (var assembly in copyAssemblies.ToList())
{
foreach (var assemblyType in assembly.GetExportedTypes())
{
int index = assembly.GetExportedTypes().ToList().IndexOf(assemblyType);
var implementsInterface = typeof(IPluginContract).IsAssignableFrom(assemblyType) && assemblyType.IsClass;
//If class assembly does not have IPluginContract, remove assembly from list
if (!implementsInterface && !assemblyType.IsInterface){
assemblies.RemoveAt(index);
}
}
}
}
}