简体   繁体   English

如何将带有dll的应用程序从内存加载到AppDomain并执行它?

[英]How do I load an application with dlls from memory into an AppDomain and execute it?

I have several streams with an assembly and its used dlls. 我有几个带有程序集和使用过的dll的流。 How can I load them into an AppDomain and execute the main assembly? 如何将它们加载到AppDomain并执行主程序集? I'd rather not save the files to disk if it can be avoided. 如果可以避免,我宁愿不将文件保存到磁盘。

You can use obtain the assembly through the following mechanism. 您可以通过以下机制使用获取程序集。

Assembly myAssembly = Assembly.Load(<your raw file stream>);

You can register for the following event and handle the same to serve requested types coming from your custom assemblies: 您可以注册以下事件并处理相同的事件以提供来自自定义程序集的请求类型:

AppDomain.CurrentDomain.TypeResolve += new ResolveEventHandler(CurrentDomain_TypeResolve);

static Assembly CurrentDomain_TypeResolve(object sender, ResolveEventArgs args)
    {
        Type  resolvedType =  myAssembly.GetType( args.Name, false);
    }

Unfortunately any type loaded in your program would end up here, so you might want to build in some caching mechanism to store type info 不幸的是,程序中加载的任何类型都会在这里结束,因此您可能希望构建一些缓存机制来存储类型信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM