Assembly.LoadFile is non-compliant?


I recently followed an example for pre-loading several DLLs from the bin folder as described here:

This actually did work as expected, but I am using SonarLint for VisualStudio and in the following line of code is underlinded and marked as a "Code Smell":


Stating the following S3885 (

The parameter to Assembly.Load includes the full specification of the dll to be loaded. Use another method, and you might end up with a dll other than the one you expected.

This rule raises an issue when Assembly.LoadFrom, Assembly.LoadFile, or Assembly.LoadWithPartialName is called.

So I gave it a try and changed it into Assembly.Load(dll); as recommended:

private const string BinFolderName = "bin";
public static void LoadAllBinDirectoryAssemblies(string fileMask)

    string binPath = System.AppDomain.CurrentDomain.BaseDirectory;

    if(Directory.Exists(Path.Combine(binPath, BinFolderName)))
        binPath = Path.Combine(binPath, BinFolderName);

    foreach (string dll in Directory.GetFiles(binPath, fileMask, SearchOption.AllDirectories))
            Assembly.Load(dll); // Before: Assembly.LoadFile
        catch (FileLoadException ex)
            // The Assembly has already been loaded.
        catch (BadImageFormatException)
            // If a BadImageFormatException exception is thrown, the file is not an assembly.


But using the recommended method an FileLoadException is thrown:

Could not load file or assembly 'C:\SomePath\SomeDll.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

The reason: The string of Assembly.Load is not a file path, it is actually a class name such as "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3".

Is this simply a false-positive from SonarLint or does a "compliant" way exist?

asked on Stack Overflow Oct 14, 2018 by DominikAmon • edited Oct 14, 2018 by DominikAmon

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0