【发布时间】:2020-04-12 09:34:25
【问题描述】:
我正在尝试以编程方式从 nuget 包中提取 dll 并在运行时加载 dll。
我想避免使用任何命令行工具 - 我希望我的程序完全独立,而不是依赖外部可执行文件。
我正在尝试使用https://www.nuget.org/profiles/nuget 中列出的各种 nuget.client nuget 包,但没有任何文档,我不知道如何使用。
我有 nupkg,我可以通过 PackageReader 计算出 nupkg 中 dll 的位置,但我不知道如何提取 nupkg 以便将文件取出。
编辑
感谢那些指出 nupkg 只是一个 zip 的人。我现在做了以下事情:
var archive = new ZipArchive(downloadResourceResult.PackageStream);
var entry = archive.GetEntry(dllPath);
var assemblyLoadContext = new System.Runtime.Loader.AssemblyLoadContext(null, isCollectible: true);
var assembly = assemblyLoadContext.LoadFromStream(entry.Open());
但是这会引发 NotSupportedException 并带有以下堆栈跟踪
System.IO.Compression.DeflateStream.get_Length() at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly, Stream assemblySymbols) at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly)
【问题讨论】:
-
提示:它是一个 zip 文件
-
nupkg 文件只是 zip 文件。只需像往常一样解压缩,然后查看相应的子文件夹...
-
好的,我已经做到了。现在,当我尝试从 zip 文件流中加载它时,我从 System.IO.Compression.DeflateStream.get_Length() 得到一个 NotSupportedException