简体   繁体   中英

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?

The AppDomain.Load(byte[]) does not work since it's giving me FileNotFoundException, I assume that the assembly must be on my computer.

        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.

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. Search for "C# LoadFrom Cook" to get to set of articles by Suzanne Cook about loading assemblies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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