从内存加载引用的DLL [英] Loading Referenced DLLs from Memory

查看:96
本文介绍了从内存加载引用的DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我有一个应用程序(让我们称之为 LoaderApp )将一个byte []读入内存并加载它作为一个程序集(假设byte []是一个可执行文件。这很好。但是如何设置 LoaderApp 也将byte []的引用DLL加载到它的Assembly中? br />


到目前为止的代码如下:



  byte  [] bytes =;  //  此处加载可执行字节。 。 

汇编汇编 = Assembly.Load(字节);
MethodInfo方法= assembly .EntryPoint;

if (method!= null
{
object o = assembly .CreateInstance(method .Name);
尝试
{
method.Invoke(o, null );
}
catch (TargetInvocationException e)
{
// TODO:处理异常
}
}





我已经读过加载的程序集可以自己搜索它引用的DLLS,如下所述:在运行时加载程序集方法#3。



但是由于 LoaderApp 将加载多个可执行的可执行文件,我宁愿放置逻辑在其中,而不是每个可执行文件...



非常感谢:)

解决方案

你好,



要获得特定类型的引用程序集,可以尝试使用下面的代码。

 typeof (Item).Assembly)。GetReferencedAssemblies()







谢谢,

不是最好的方法,但由于没有其他人提供任何建议,这就是我做的:



1. LoaderApp 在内存中加载所有引用的程序集(如原始问题的代码片段所示)
2.加载的程序集传递给动态加载的私有方法只接受(并存储)Assembly []集合的程序集。

3.我已添加到动态加载的assmeblies:

 AppDomain.CurrentDomain.AssemblyResolve + =  new  ResolveEventHandler(MyResolveEventHandler); 



4.对于每个未解析的程序集,动态程序集检查其Assembly []集合并返回匹配的集合作为正确的引用程序集。


Hey guys,

I have an application (let's call it LoaderApp) that reads a byte[] into memory and loads it as an assembly (provided that the byte[] is an executable. This works fine. But how can I set the LoaderApp to also load the referenced DLLs of the byte[] into its Assembly as well?

The code so far is as follows:

byte[] bytes = ; // Executable bytes are loaded here...

Assembly assembly = Assembly.Load(bytes);
MethodInfo method = assembly.EntryPoint;

if (method != null)
{
    object o = assembly.CreateInstance(method.Name);
    try
    {
        method.Invoke(o, null);
    }
    catch (TargetInvocationException e)
    {
        // TODO: Handle exception
    }
}



I have read that the loaded assembly can search for its referenced DLLS by itself, as described here: Load Assembly at Runtime Method #3.

But since the LoaderApp will be loading more than one possible executable, I'd rather have that logic placed in it, rather than for every executable...

Thanks a lot :)

解决方案

Hi,

To get referenced assemblies frorm a specific type, you can try using the code below.

(typeof(Item).Assembly).GetReferencedAssemblies()




Thank you,


Not the best way to do it, but since no one else provided any suggestions, here's what I did:

1. The LoaderApp loads all the referenced assemblies in memory (as shown in the code snippet of the original question
2. The loaded Assemblies are passed to a private method implemented in the dynamically loaded assemblies which simply accepts (and stores) an Assembly[] collection.
3. I've added to the dynamically loaded assmeblies:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);


4. For each unresolved assembly, the dynamic assembly checks its Assembly[] collection and returns the matching one as the correct referenced assembly.


这篇关于从内存加载引用的DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆