简体   繁体   English

从本地 Nuget 包文件以编程方式加载程序集

[英]Programmatically Load Assembly From Local Nuget Package File

On my local file System I have the nuget package foo.nupkg .在我的本地文件系统上,我有 nuget 包foo.nupkg It contains the assembly foo.dll .它包含程序集foo.dll

If I manually extract the foo.nupkg I can programmatically load the foo.dll assembly as follows:如果我手动提取foo.nupkg我可以按如下方式以编程方式加载foo.dll程序集:

AssemblyLoadContext.Default.LoadFromAssemblyPath("c:/path/foo.dll");

How can I load the assembly from foo.nupkg directly?如何直接从foo.nupkg加载程序集?

What I already tried:我已经尝试过的:

  • Of course I can programmatically extract the .nupkg file to a temporary directory but I wonder if this is really required.当然,我可以以编程方式将.nupkg文件提取到临时目录,但我想知道这是否真的需要。
  • I tried using package NuGet.Core .我尝试使用包NuGet.Core I provides a PackageManager#InstallPackage that accepts an IPackage .我提供了一个接受IPackagePackageManager#InstallPackage But it seems that IPackage instances can only be revtrieved from repositories (which I don't have).但似乎IPackage实例只能从存储库(我没有)中恢复。

A nuget package is just a zip file, so you could simply use System.IO.Compression.ZipArchive to read it. nuget 包只是一个 zip 文件,因此您可以简单地使用 System.IO.Compression.ZipArchive 来读取它。

You can also load an assembly from a byte array, so something like this should work (assuming newtonsoft.json package is in the working directory):您还可以从字节数组加载程序集,因此应该可以使用以下内容(假设 newtonsoft.json 包在工作目录中):

using var archive = ZipFile.OpenRead("newtonsoft.json.12.0.3.nupkg");
var entry = archive.GetEntry("lib/netstandard2.0/Newtonsoft.Json.dll");

using var target = new MemoryStream();

using var source = entry.Open();
source.CopyTo(target);

var assembly = Assembly.Load(target.ToArray());
var type = assembly.GetType("Newtonsoft.Json.JsonConvert");
Console.WriteLine(type.FullName);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NuGet package 因“无法加载文件或程序集”而中断 - NuGet package breaks with "Could not load file or assembly" Nuget包引用导致“无法加载文件或程序集”? - Nuget package references cause “Could not load file or assembly” ? Elmah MySql Nuget包问题'无法加载文件或程序集' - Elmah MySql Nuget Package Issue 'Could not load file or assembly' 无法为 NET Standard 加载 NuGet 包的文件或程序集异常 NET 5/6 - could not load file or assembly exception NET 5/6 of NuGet package for NET Standard Publish Azure Function as NuGet package (or load function from external assembly) - Publish Azure Function as NuGet package (or load function from external assembly) 来自 NuGet package 的运行时异常引用类型:'无法加载文件或程序集'Foo'。 该系统找不到指定的文件。' - Runtime exception referencing type from a NuGet package: 'Could not load file or assembly 'Foo'. The system cannot find the file specified.' PDFClown nuget软件包无法加载程序集 - PDFClown nuget package cannot load assembly 无法加载文件或程序集 ICSharpCode.SharpZipLib... 当使用 nuGet 包 ExcelDataReader - Could not load file or assembly ICSharpCode.SharpZipLib… When using nuGet package ExcelDataReader 使用NHUnspell NuGet包时无法加载文件或程序集Hunspellx64.dll? - Could not load file or assembly Hunspellx64.dll while using NHUnspell NuGet package? 安装MVC论坛Nuget包后无法加载文件或程序集'Ninject.Web' - Could not load file or assembly 'Ninject.Web' after Install MVC Forum Nuget Package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM