如何在 Assembly.LoadFile() 之前从 DLL 中读取属性
How to read properties from DLL before Assembly.LoadFile()
我的程序有一个pluginManager模块,它可以加载一个DLL文件和运行 DLL的方法,但是我需要在Assembly.LoadFile()
之前读取DLL的属性。我该怎么办?
我读过有关 Assembly 的文档,它们在 Assembly.LoadFile()
之后读取属性,你知道 Assembly 没有 UnLoad() 方法,所以我必须在 LoadFile() 之前读取属性
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
方法创建应用域。无论如何,你应该为插件做这个,这样你就可以在完成后卸载它们。
FileVersionInfo.GetVersionInfo
正是您要找的。
试试这个代码
AppDomain.CurrentDomain.AssemblyResolve += (发件人, bargs) =>
{
String dllName = new AssemblyName(bargs.Name).Name + ".dll";
var assem = Assembly.GetExecutingAssembly();
String resourceName = assem.GetManifestResourceNames().FirstOrDefault(rn => rn.EndsWith(dllName));
if (resourceName == null) return null; // For null assembly
using (var stream = assem.GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
我的程序有一个pluginManager模块,它可以加载一个DLL文件和运行 DLL的方法,但是我需要在Assembly.LoadFile()
之前读取DLL的属性。我该怎么办?
我读过有关 Assembly 的文档,它们在 Assembly.LoadFile()
之后读取属性,你知道 Assembly 没有 UnLoad() 方法,所以我必须在 LoadFile() 之前读取属性
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
方法创建应用域。无论如何,你应该为插件做这个,这样你就可以在完成后卸载它们。
FileVersionInfo.GetVersionInfo
正是您要找的。
试试这个代码
AppDomain.CurrentDomain.AssemblyResolve += (发件人, bargs) => {
String dllName = new AssemblyName(bargs.Name).Name + ".dll";
var assem = Assembly.GetExecutingAssembly();
String resourceName = assem.GetManifestResourceNames().FirstOrDefault(rn => rn.EndsWith(dllName));
if (resourceName == null) return null; // For null assembly
using (var stream = assem.GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};