I have an application, that loads all the .dlls the user puts into a special directory. If they are created wrong (x86,x64) or something else, an BadImageFormatException is thrown. Thats ok, but I don't like avoid exceptions. So the question is:
Is there a way to analyse the file in advance to NOT get an exception?
- Check if it's the right platform target
- Check if it's unmanaged..
-
- other possible issues.
Main goal is to avoid the exception. It would also be OK to get a bool that tells, if it will work.
Current Code:
private Assembly TryLoadAssmbly(string file)
{
Assembly result;
try
{
result = Assembly.LoadFile(file);
}
catch (BadImageFormatException exc)
{
Log.Error($"File \"{file}\" could not be loaded");
result = null;
}
return result;
}