开发者

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

开发者 https://www.devze.com 2023-01-10 08:38 出处:网络
I have开发者_如何转开发 several streams with an assembly and its used dlls. How can I load them into an AppDomain and execute the main assembly? I\'d rather not save the files to disk if it can be avo

I have开发者_如何转开发 several streams with an assembly and its used dlls. How can I load them into an AppDomain and execute the main assembly? 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

0

精彩评论

暂无评论...
验证码 换一张
取 消