Renaming the assembly file and Assembly.LoadFile

So here is what:

I have an assembly called Lib1.dll. For some reason (not related to the issue) I had to rename the assembly file name to Lib1New.dll, now, while trying to load the renamed assembly using Assembly.LoadFile, I noticed that the CLR was trying to load Lib1.dll.

If Lib1.dll is in the search path, it loads into the address space. The application works fine regardless of whether Lib1.dll is found or not. (The problem is that if Lib1.dll is detected, the file is locked and cannot be deleted by other processes).

enter image description here

I do not understand why LoadFile is looking for and loading Lib1.dll. LoadFile should load the contents of the assembly file to the specified location, why it is looking for files.

MSDN documentation for LoadFile:

Use the LoadFile method to load and verify assemblies with the same identifier, but they are located in different ways. LoadFile does not load files into the LoadFrom context and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario, because LoadFrom cannot be used to load assemblies with the same identifiers, but with different paths; it will only load the first such assembly.

+5
source share
2 answers

, . Lib.dll, , , LoadFile, Lib.dll "LibNew.dll". lib.dll . LibNew.dll, Lib.dll.

, Lib.dll Load , Lib.dll. DLL, . DLL , , . :

:

class Program
{
    static void Main(string[] args)
    {
        System.Reflection.Assembly assy = System.Reflection.Assembly.LoadFile(args[0]);
        Type class1 = assy.GetType("Lib.Class1");

        System.Reflection.MethodInfo myMethod = class1.GetMethod("MyMethod");
        Console.WriteLine(myMethod.Invoke(null, new object[] {"This is a string"}).ToString());

        Console.ReadLine();
    }
}

Lib:

namespace Lib
{
public class Class1
{
    public static string MyMethod(string param)
    {
        return "Fixed: [" + param.Replace(" ", "-") + "]";
    }
}
}

LoadImage, , Lib.dll: Process Monitor Trace of LoadImage calls

, , , Lib.dll . Process Monitor Trace of all references to Debug \ Lib

, DLL , , ? , , .

+2

LoadFile Windows, .NET. .

new FileStream(), _, PATH .., .

+1

All Articles