简体   繁体   中英

How to load assemblies in different folders C#

I need load a DLL and dependencies. In my database I already save all dependencies (path to the references files).

IE:

DLL to load:

  • id: 1
  • name:"DummyModule.dll"

Dependencies:

  • DLL id: 1
  • path: "C:\\DLL\\ABC.dll"

AssemblyLoader class:

public class AssemblyLoader : MarshalByRefObject
{
    public void Load(string path)
    {
        ValidatePath(path);

        Assembly.Load(path);
    }

    public void LoadFrom(string path)
    {
        ValidatePath(path);

        Assembly.LoadFrom(path);
    }

    public void LoadBytes(string path)
    {
        ValidatePath(path);

        var b = File.ReadAllBytes(path);

        Assembly.Load(b);
    }

    public Assembly GetAssembly(string assemblyPath)
    {
        try
        {
            return Assembly.Load(assemblyPath);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(ex.Message);
        }
    }

    public Assembly GetAssemblyBytes(string assemblyPath)
    {
        try
        {
            var b = File.ReadAllBytes(assemblyPath);

            return Assembly.Load(b);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(ex.Message);
        }
    }

    private void ValidatePath(string path)
    {
        if (path == null)
            throw new ArgumentNullException("path");

        if (!File.Exists(path))
            throw new ArgumentException(String.Format("path \"{0}\" does not exist", path));
    }
}

The main class:

    static void Main(string[] args)
    {
        string file1 = @"1\DummyModule.dll";
        string file2 = @"2\PSLData.dll";
        string file3 = @"3\Security.dll";

        try
        {
            AppDomain myDomain = AppDomain.CreateDomain("MyDomain");

            var assemblyLoader = (AssemblyLoader)myDomain.CreateInstanceAndUnwrap(typeof(AssemblyLoader).Assembly.FullName, typeof(AssemblyLoader).FullName);

            assemblyLoader.LoadBytes(file2);
            assemblyLoader.LoadBytes(file3);

            var dummy = assemblyLoader.GetAssemblyBytes(file1);

            foreach (var t in dummy.GetTypes())
            {
                var methodInfo = t.GetMethod("D");
                if (methodInfo != null)
                {
                    var obj = Activator.CreateInstance(t);
                    Console.Write(methodInfo.Invoke(obj, new object[] { }).ToString());
                }
            }

            AppDomain.Unload(myDomain);
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }

        Console.ReadKey();
    }

In the code above the "DummyModule.dll" is the main dll, "PSLData.dll" and "Security.dll" are the dependencies.

When I call the method "D" of my "DummyModule.dll" the error appears:

Could not load file or assembly 'DummyModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

All DLL files still in different folders. How I can load all needed files and call a function?

Thanks.

Try using this.. it worked for me..

serviceAgentAssembly =System.Reflection.Assembly.LoadFrom(string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", assemblyPath, assemblyName));

foreach (Type objType in serviceAgentAssembly.GetTypes())
{
  //further loops to get the method
  //your code to ivoke the function
}

You're using relative paths to the assemblies, so the question is "relative to what?" The new AppDomain you've created and are loading assemblies into is lost in the woods; it doesn't inherit the same probe paths of the AppDomain in which you created it. Take a look at the class System.AppDomainSetup and its properties ApplicationBase and PrivateBinPath along with the form of CreateDomain() that takes an instance of AppDomainSetup as an argument. The simplest solution would be to use the AppDomainSetup instance returned by AppDomain.CurrentDomain.SetupInformation were the current domain is, of course, the app in which the new domain is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM