[英]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:我已经尝试过的:
.nupkg
file to a temporary directory but I wonder if this is really required..nupkg
文件提取到临时目录,但我想知道这是否真的需要。NuGet.Core
.NuGet.Core
。 I provides a PackageManager#InstallPackage
that accepts an IPackage
.IPackage
的PackageManager#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.