示例#1
0
        public void AssemblySandboxLoadType()
        {
            var sandbox1 = new AssemblySandbox();

            try
            {
                Type.GetType("System.Windows.UIElement", true, false);
                Assert.Fail();
            }
            catch (TypeLoadException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            try
            {
                sandbox1.GetTypeRef("System.Windows.UIElement");
                Assert.Fail();
            }
            catch (TypeLoadException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

            try
            {
                Type.GetType("System.Windows.UIElement", true, false);
                Assert.Fail();
            }
            catch (TypeLoadException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            Assert.AreEqual(1, sandbox1.GetAssemblies().Count);
            Assert.IsNotNull(sandbox1.GetTypeRef("System.Windows.UIElement", "PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"));
            Assert.AreEqual(1, sandbox1.GetAssemblies().Count);
            Assert.IsNotNull(sandbox1.GetTypeRef("System.Windows.UIElement"));
            Assert.AreEqual(6, sandbox1.GetAssemblies().Count);

            sandbox1.Dispose();
        }
示例#2
0
        public void AssemblySandboxLoadFromPath()
        {
            var sandbox1 = new AssemblySandbox();
            var sandbox2 = new AssemblySandbox();

            Assert.AreEqual(0, sandbox1.GetAssemblies().Count);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.LoadAssembly(@"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll");

            Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

            Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.Dispose();
            sandbox2.Dispose();
        }
示例#3
0
        public void AssemblySandboxLoadFromFullName()
        {
            var sandbox1 = new AssemblySandbox();
            var sandbox2 = new AssemblySandbox();

            Assert.AreEqual(0, sandbox1.GetAssemblies().Count);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

            Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.LoadAssembly("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");

            Assert.AreEqual("PresentationCore", sandbox1.GetAssemblies().Single().Name);
            Assert.AreEqual(0, sandbox2.GetAssemblies().Count);

            sandbox1.Dispose();
            sandbox2.Dispose();
        }
示例#4
0
        /// <summary>
        /// Gets a reference to the required assemblies.
        /// </summary>
        /// <returns>A list of reference to the assemblies.</returns>
        private IReadOnlyList <MetadataReference> GetAssemblyReferences()
        {
            var references = new List <MetadataReference>();

            references.Add(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

            foreach (var assembly in _assemblySandbox.GetAssemblies().Where(a => a.IsDotNetAssembly))
            {
                references.Add(MetadataReference.CreateFromFile(assembly.Location));
            }

            return(references.AsReadOnly());
        }
示例#5
0
        /// <summary>
        /// Compiles the program in memory and save it on the hard drive or returns the build errors.
        /// </summary>
        /// <param name="callback">The cross-AppDomain task proxy.</param>
        /// <param name="outputType">Defines the type of assembly to generate</param>
        /// <param name="outputPath">The full path to the .exe or .dll file to create if the build succeed.</param>
        /// <param name="assemblyName">Defines the name of the assembly to generate.</param>
        /// <param name="assemblyVersion">Defines the version of the assembly.</param>
        /// <param name="assemblyCopyright">Defines the copyright of the assembly.</param>
        /// <param name="releaseMode">Defines whether the compiler must build in release mode or debug mode.</param>
        /// <returns>Returns the build errors, or null if it succeed.</returns>
        internal void Build(MarshaledResultSetter <AggregateException> callback, BaZicCompilerOutputType outputType, string outputPath, string assemblyName, string assemblyVersion, string assemblyCopyright, bool releaseMode)
        {
            Requires.NotNull(_middleware, nameof(_middleware));
            Requires.NotNullOrWhiteSpace(outputPath, nameof(outputPath));

            if (DebugMode)
            {
                throw new UnexpectedException(new Exception(L.BaZic.Runtime.BaZicInterpreter.CannotBuildAfterStartDebug));
            }
            CheckState(BaZicInterpreterState.Ready, BaZicInterpreterState.Stopped, BaZicInterpreterState.StoppedWithError);

            if (ProgramIsOptimized)
            {
                throw new InvalidOperationException(L.BaZic.Runtime.BaZicInterpreter.CannotRunOptimizedProgramInRelease);
            }

            Error = null;
            ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Running));

            AggregateException buildErrors = null;
            var currentCulture             = LocalizationHelper.GetCurrentCulture();

            _mainInterpreterTask = Task.Run(() =>
            {
                LocalizationHelper.SetCurrentCulture(currentCulture, false, false);

                var outputFile = new FileInfo(outputPath);
                var directory  = outputFile.Directory;

                if (string.IsNullOrWhiteSpace(outputFile.Extension))
                {
                    if (outputType == BaZicCompilerOutputType.DynamicallyLinkedLibrary)
                    {
                        outputFile = new FileInfo(outputPath + ".dll");
                    }
                    else
                    {
                        outputFile = new FileInfo(outputPath + ".exe");
                    }
                }

                var outputFileName = Path.GetFileNameWithoutExtension(outputPath);
                var outputPdbFile  = new FileInfo(Path.Combine(directory.FullName, outputFileName + ".pdb"));

                using (var compileResult = Build(outputType, assemblyName, assemblyVersion, assemblyCopyright, releaseMode))
                {
                    if (compileResult.BuildErrors != null)
                    {
                        buildErrors = compileResult.BuildErrors;
                        ChangeState(this, new UnexpectedException(buildErrors));
                        return;
                    }

                    try
                    {
                        if (!directory.Exists)
                        {
                            directory.Create();
                        }

                        var assembliesToCopy = _assemblySandbox.GetAssemblies().Where(a => a.CopyToLocal).Concat(Program.Assemblies.Where(a => a.CopyToLocal)).Distinct();
                        foreach (var assembly in assembliesToCopy)
                        {
                            if (File.Exists(assembly.Location))
                            {
                                File.Copy(assembly.Location, Path.Combine(directory.FullName, Path.GetFileName(assembly.Location)));

                                var pdbFilePath = Path.Combine(Directory.GetParent(assembly.Location).FullName, Path.GetFileNameWithoutExtension(assembly.Location) + ".pdb");
                                var xmlFilePath = Path.Combine(Directory.GetParent(assembly.Location).FullName, Path.GetFileNameWithoutExtension(assembly.Location) + ".xml");

                                if (File.Exists(pdbFilePath))
                                {
                                    File.Copy(pdbFilePath, Path.Combine(directory.FullName, Path.GetFileName(pdbFilePath)));
                                }

                                if (File.Exists(xmlFilePath))
                                {
                                    File.Copy(xmlFilePath, Path.Combine(directory.FullName, Path.GetFileName(xmlFilePath)));
                                }
                            }
                        }

                        using (var assemblyFileStream = new FileStream(outputFile.FullName, FileMode.Create))
                            using (var pdbFileStream = new FileStream(outputPdbFile.FullName, FileMode.Create))
                            {
                                compileResult.Assembly.WriteTo(assemblyFileStream);
                                compileResult.Pdb.WriteTo(pdbFileStream);
                            }

                        ChangeState(this, new BaZicInterpreterStateChangeEventArgs(BaZicInterpreterState.Stopped));
                    }
                    catch (Exception exception)
                    {
                        CoreHelper.ReportException(exception);
                        buildErrors = new AggregateException(new List <Exception> {
                            exception
                        });
                        ChangeState(this, new UnexpectedException(exception));
                    }
                }
            });
            _mainInterpreterTask.ContinueWith((task) =>
            {
                callback.SetResult(buildErrors);
            });
        }