簡體   English   中英

Asp.net核心負載組件

[英]Asp.net Core Load Assembly

我正在嘗試從文件夾中加載一些.dll文件:

    var fileNames = Directory
        .GetFiles(path, "*.dll", searchOption);
    var assemblyNames = fileNames
        .Select(AssemblyLoadContext.GetAssemblyName);

    List<Assembly> assemblies = new List<Assembly>();
    foreach (AssemblyName assemblyName in assemblyNames)
    {
        assemblies.Add(Assembly.Load(assemblyName));
    }

但是FileNotFoundException, Could not load file or assembly [...] The system cannot find the file specified.以某種方式加載程序集: FileNotFoundException, Could not load file or assembly [...] The system cannot find the file specified.

因為文件肯定存在,這怎么可能?

如果您需要更多背景知識,我可以提供其他信息。

使用這些using語句:

using System.Reflection;
using System.Runtime.Loader;

嘗試這個:

var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(pathTodll);

要從文件加載程序集,使用Assembly.LoadFrom()函數會更容易。 在您的應用程序中將如下所示:

var fileNames = Directory.GetFiles(path, "*.dll", searchOption);

List<Assembly> assemblies = new List<Assembly>(); foreach (string fileName in fileNames) { assemblies.Add(Assembly.LoadFrom(fileName)); }

[編輯]

如果您嘗試加載程序集以供以后在應用程序中使用(例如,您已經在Visual Studio中引用了程序集),建議您實現AppDomain.AssemblyResolve事件。 當找不到程序集並需要加載程序集時,將觸發AppDomain.AssemblyResolve事件。 那時,您可以在其他文件位置為AppDomain提供程序集。

暫無
暫無

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

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