0xFF
0xFF

Reputation: 177

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();

Upvotes: 2

Views: 2303

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100630

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.

Upvotes: 1

Related Questions