简体   繁体   English

将Byte []程序集加载到新的AppDomain中

[英]Load Byte[] Assembly into new AppDomain

I currently get an assembly as a byte array from a remote stream. 我目前从远程流获得一个程序集作为字节数组。 Is there anyway to load it the into a new AppDomain? 无论如何,将其加载到新的AppDomain中?

The AppDomain.Load(byte[]) does not work since it's giving me FileNotFoundException, I assume that the assembly must be on my computer. AppDomain.Load(byte [])不起作用,因为它给了我FileNotFoundException,我假设程序集必须在我的计算机上。

        AppDomain domain = AppDomain.CreateDomain("Test");

        Thread t = new Thread(() =>
        {
            Assembly assembly = domain.Load(bytes);
            MethodInfo method = assembly.EntryPoint;
            if (method != null)
            {
                object o = assembly.CreateInstance(method.Name);
                try
                {
                    method.Invoke(o, null);
                }
                catch (TargetInvocationException ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        });
        t.Start();

You need to pass that byte array to code running in new AppDomain and call Load(byte[]) on that data. 您需要将该字节数组传递给在新AppDomain中运行的代码,然后对该数据调用Load(byte [])

Now as with any loading of an assembly you need to understand how dependencies are resolved when useing different methods of loading assemblies. 现在,与加载程序集一样,您需要了解在使用不同的加载程序集方法时如何解决依赖关系。 In most cases you'll have to either preload dependencies into new AppDomain or add AssemblyResolver event handler. 在大多数情况下,您必须将依赖项预加载到新的AppDomain中,或添加AssemblyResolver事件处理程序。 Search for "C# LoadFrom Cook" to get to set of articles by Suzanne Cook about loading assemblies. 搜索“ C#LoadFrom Cook”,以获取Suzanne Cook关于加载程序集的文章集。

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

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