Ejemplo n.º 1
0
        /// <summary>
        ///   Compiles the <paramref name="compilation" /> with the S# compiler and returns the resulting assembly that has been
        ///   loaded into the app domain.
        /// </summary>
        /// <param name="compilation">The compilation that should be compiled.</param>
        /// <param name="output">The output that should be used to write test output.</param>
        public static Assembly CompileSafetySharp(Compilation compilation, TestTraceOutput output)
        {
            using (var workspace = new AdhocWorkspace())
            {
                var project = workspace
                              .AddProject(compilation.AssemblyName, LanguageNames.CSharp)
                              .AddMetadataReferences(compilation.References)
                              .WithCompilationOptions(compilation.Options);

                foreach (var syntaxTree in compilation.SyntaxTrees)
                {
                    project = project.AddDocument(syntaxTree.FilePath, syntaxTree.GetRoot().GetText(Encoding.UTF8)).Project;
                }

                var errorReporter = new TestErrorReporter(output);
                var compiler      = new Compiler(errorReporter);

                try
                {
                    var assembly = compiler.Compile(project);
                    output.Trace("{0}", SyntaxTreesToString(compiler.Compilation));

                    return(assembly);
                }
                catch (CompilationException e)
                {
                    throw new TestException("{0}\n\n{1}", e.Message, SyntaxTreesToString(compiler.Compilation));
                }
            }
        }
Ejemplo n.º 2
0
		/// <summary>
		///   Compiles the <paramref name="compilation" /> with the S# compiler and returns the resulting assembly that has been
		///   loaded into the app domain.
		/// </summary>
		/// <param name="compilation">The compilation that should be compiled.</param>
		/// <param name="output">The output that should be used to write test output.</param>
		public static Assembly CompileSafetySharp(Compilation compilation, TestTraceOutput output)
		{
			using (var workspace = new AdhocWorkspace())
			{
				var project = workspace
					.AddProject(compilation.AssemblyName, LanguageNames.CSharp)
					.AddMetadataReferences(compilation.References)
					.WithCompilationOptions(compilation.Options);

				foreach (var syntaxTree in compilation.SyntaxTrees)
					project = project.AddDocument(syntaxTree.FilePath, syntaxTree.GetRoot().GetText(Encoding.UTF8)).Project;

				var errorReporter = new TestErrorReporter(output);
				var compiler = new Compiler(errorReporter);

				try
				{
					var assembly = compiler.Compile(project);
					output.Trace("{0}", SyntaxTreesToString(compiler.Compilation));

					return assembly;
				}
				catch (CompilationException e)
				{
					throw new TestException("{0}\n\n{1}", e.Message, SyntaxTreesToString(compiler.Compilation));
				}
			}
		}