public static AppDomain Assemble(
            string friendlyName,
            AppDomainResolutionPaths resolutionPaths)
        {
            if (resolutionPaths == null)
            {
                throw new ArgumentNullException("resolutionPaths");
            }

            var domain = Create(friendlyName, resolutionPaths);

            // Attach to the assembly file resolve event
            // We check for a null reference but not for an empty one,
            // there is after all no reason why the collection couldn't fill up later.
            if (resolutionPaths.Files != null)
            {
                var resolver = Activator.CreateInstanceFrom(
                    domain,
                    typeof(FileBasedResolver).Assembly.LocalFilePath(),
                    typeof(FileBasedResolver).FullName).Unwrap() as FileBasedResolver;

                Debug.Assert(resolver != null, "Somehow we didn't create a resolver.");
                resolver.StoreFilePaths(resolutionPaths.Files);
                resolver.Attach();
            }

            // Attach to the assembly directory resolve event
            // We check for a null reference but not for an empty one,
            // there is after all no reason why the collection couldn't fill up later.
            if (resolutionPaths.Directories != null)
            {
                var resolver = Activator.CreateInstanceFrom(
                    domain,
                    typeof(DirectoryBasedResolver).Assembly.LocalFilePath(),
                    typeof(DirectoryBasedResolver).FullName).Unwrap() as DirectoryBasedResolver;

                Debug.Assert(resolver != null, "Somehow we didn't create a resolver.");
                resolver.StoreDirectoryPaths(resolutionPaths.Directories);
                resolver.Attach();
            }

            // Attach the exception handler
            {
                var attacher = Activator.CreateInstanceFrom(
                    domain,
                    typeof(ExceptionHandlerAttacher).Assembly.LocalFilePath(),
                    typeof(ExceptionHandlerAttacher).FullName).Unwrap() as ExceptionHandlerAttacher;

                Debug.Assert(attacher != null, "Somehow we didn't create an exception handler attacher.");
                attacher.Attach();
            }

            return(domain);
        }
        private static AppDomain Create(string name, AppDomainResolutionPaths resolutionPaths)
        {
            {
                Debug.Assert(resolutionPaths != null, "The base path must be defined");
            }

            var setup = new AppDomainSetup
            {
                ApplicationName      = Assembly.GetCallingAssembly().GetName().Name,
                ApplicationBase      = resolutionPaths.BasePath,
                ShadowCopyFiles      = "false",
                DisallowCodeDownload = true
            };

            var result = AppDomain.CreateDomain(
                string.IsNullOrEmpty(name) ? GenerateNewAppDomainName() : name,
                null,
                setup);

            return(result);
        }