gpt4 book ai didi

c# - 如何在 Assembly.LoadFile() 之前从 DLL 中读取属性

转载 作者:太空狗 更新时间:2023-10-29 21:38:31 25 4
gpt4 key购买 nike

我的程序有一个 pluginManager 模块,它可以加载 DLL 文件并运行 DLL 的方法,但我需要在 Assembly.LoadFile() 之前读取 DLL 属性。我该怎么办?

我读过关于 Assembly 的文档,它们在 Assembly.LoadFile() 之后读取属性,你知道 Assembly 没有 UnLoad() 方法,所以我必须在 LoadFile() 之前读取属性 enter image description here

    private void ImprotZip(string path)
{
/*
1、create tempDir, uppackage to tempDir
2、Load Plugin DLL, Load plugin dependent lib DLL
*/
string tempDirectory = CreateRuntimeDirectory(path);
string[] dllFiles = Directory.GetFiles(tempDirectory);
///Load DlL
foreach(string dll in dllFiles)
{
ImprotDll(dll, false);
}
string libPath = string.Format("{0}\\lib\\", tempDirectory);
if (!Directory.Exists(libPath))
return;
string[] dlls = Directory.GetFiles(libPath);
///Load plugin dependent lib DLL
foreach(string dll in dlls)
{
try
{
//filtering same DLL
//if(Dll.properties.AssemblyProduct != "something")
//continue;
Assembly.LoadFile(dll);
}
catch(Exception e)
{
e.Log();
}
}
}

最佳答案

您可以使用 Assembly.ReflectionOnlyLoad 将程序集加载到仅反射上下文中.

我仍然会为仅反射上下文创建一个应用程序域,这样您就可以在完成后卸载该域。您可以使用 AppDomain.CreateDomain 创建应用域方法。无论如何,您应该对插件执行此操作,以便在完成后卸载它们。

关于c# - 如何在 Assembly.LoadFile() 之前从 DLL 中读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com