簡體   English   中英

如何在不同文件夾中加載程序集C#

[英]How to load assemblies in different folders C#

我需要加載一個DLL和依賴項。 在我的數據庫中,我已經保存了所有依賴項(引用文件的路徑)。

IE瀏覽器:

DLL加載:

  • 編號:1
  • 名稱:“ DummyModule.dll”

依存關系:

  • DLL ID:1
  • 路徑:“ C:\\ DLL \\ ABC.dll”

AssemblyLoader類:

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));
    }
}

主班:

    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();
    }

在上面的代碼中,“ DummyModule.dll”是主要的dll,“ PSLData.dll”和“ Security.dll”是依賴項。

當我調用“ DummyModule.dll”的方法“ D”時,出現錯誤:

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.

所有DLL文件仍位於不同的文件夾中。 如何加載所有需要的文件並調用函數?

謝謝。

嘗試使用它..它對我有用..

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
}

您正在使用程序集的相對路徑,因此問題是“相對於什么?” 您創建並正在將程序集加載到其中的新AppDomain丟失了; 它不會繼承您在其中創建AppDomain的相同探測路徑。 看看類System.AppDomainSetup及其屬性ApplicationBasePrivateBinPath,以及以AppDomainSetup實例為參數的CreateDomain()形式。 最簡單的解決辦法是使用由AppDomain.CurrentDomain.SetupInformation返回的AppDomainSetup實例是當前域,當然,在其中創建新域的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM