Beispiel #1
0
 public void CompileTest()
 {
     var compiler = new CSharpCompiler("test");
       var func = compiler.Compile("test.prop2 = test.prop;");
       dynamic test = new ExpandoObject();
       test.prop = "test";
       func(test, null);
       Assert.AreEqual(test.prop, test.prop2);
 }
Beispiel #2
0
 public static void Test_01()
 {
     CSharpCompiler compiler = new CSharpCompiler();
     compiler.AssemblyName = "test_01";
     compiler.OutputPath = @"test_01\test_01";
     compiler.LanguageVersion = LanguageVersion.CSharp6;
     compiler.OutputKind = OutputKind.ConsoleApplication;
     compiler.OptimizationLevel = OptimizationLevel.Debug;
     compiler.Platform = Platform.AnyCpu;
     compiler.GeneralDiagnosticOption = ReportDiagnostic.Default;
     compiler.WarningLevel = 4;
     compiler.SourceFiles = new string[] { @"test_01\test_01.cs" };
     compiler.Compile();
 }
Beispiel #3
0
 public void CompileThrowsCompilerExceptionTest()
 {
     var compiler = new CSharpCompiler("test");
       try
       {
     var func = compiler.Compile("not valid c# code");
     func(null, null);
     Assert.Fail();
       }
       catch (Exception err)
       {
     Assert.IsInstanceOfType(err, typeof(CompilerException));
       }
 }
Beispiel #4
0
        public void Compile()
        {
            CompilerSettings s = new CompilerSettings();
              CSharpCompiler c = new CSharpCompiler(s);

              ICompilerResults cr = c.CompileFiles(new string[] { @"..\..\etc\testdata\compiler\compile_ok.cs" });
              Assert.True(cr.Success);

              Assembly a = cr.GetCompiledAssemblies().First();
              Type t = a.GetType("QCV.Test.TestCompilation");
              Assert.NotNull(t);
              object o = Activator.CreateInstance(t);
              Assert.NotNull(o);
              object result = t.GetMethod("GetString").Invoke(o, null);
              Assert.NotNull(result);
              Assert.AreEqual("Compilation Succeeded", result as string);
        }
        public void CSharpCompilerShouldWorkWhenGivenValidSourceCode()
        {
            const string Source = @"using System;
class Program
{
    static void Main()
    {
        Console.WriteLine(""It works!"");
    }
}";

            var compiler = new CSharpCompiler();
            var result = compiler.Compile(CSharpCompilerPath, FileHelpers.SaveStringToTempFile(Source), string.Empty);

            Assert.IsTrue(result.IsCompiledSuccessfully, "Compilation is not successful");
            Assert.IsFalse(string.IsNullOrWhiteSpace(result.OutputFile), "Output file is null");
            Assert.IsTrue(result.OutputFile.EndsWith(".exe"), "Output file does not ends with .exe");
        }
Beispiel #6
0
 public static void Test_01()
 {
     string frameworkDir = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\";
     CSharpCompiler compiler = new CSharpCompiler();
     compiler.AssemblyName = "test_01";
     compiler.OutputPath = @"test_01\test_01.exe";
     compiler.PdbPath = @"test_01\test_01.pdb";
     compiler.LanguageVersion = LanguageVersion.CSharp6;
     compiler.OutputKind = OutputKind.ConsoleApplication;
     compiler.OptimizationLevel = OptimizationLevel.Debug;
     compiler.Platform = Platform.AnyCpu;
     compiler.GeneralDiagnosticOption = ReportDiagnostic.Default;
     compiler.WarningLevel = 4;
     compiler.SourceFiles = new string[] { @"test_01\test_01.cs" };
     // Could not find file 'C:\pib\drive\google\dev\project\.net\Test\Test.Test_01\Source\Test_Compiler\vs\Test_Roslyn_01\bin\Debug\mscorlib.dll'
     //compiler.AssembliesFiles = new string[] { "mscorlib.dll", "System.dll" };
     compiler.AssembliesFiles = new string[] { frameworkDir + "mscorlib.dll", frameworkDir + "System.dll" };
     compiler.Compile();
 }
Beispiel #7
0
 public static void Test_02()
 {
     string frameworkDir = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\";
     CSharpCompiler compiler = new CSharpCompiler();
     compiler.AssemblyName = "test_form_01";
     compiler.OutputPath = @"test_form_01\test_form_01.exe";
     compiler.PdbPath = @"test_form_01\test_form_01.pdb";
     compiler.LanguageVersion = LanguageVersion.CSharp6;
     compiler.OutputKind = OutputKind.WindowsApplication;
     compiler.OptimizationLevel = OptimizationLevel.Debug;
     compiler.Platform = Platform.AnyCpu;
     compiler.GeneralDiagnosticOption = ReportDiagnostic.Default;
     compiler.WarningLevel = 4;
     compiler.Win32ResourceFile = @"test_form_01\test.res";
     compiler.SourceFiles = new string[] { @"test_form_01\Program.cs", @"test_form_01\Form1.cs", @"test_form_01\Form1.Designer.cs", @"test_form_01\Resources.Designer.cs" };
     compiler.ResourceFiles = new ResourceFile[] { new ResourceFile { File = @"test_form_01\Test_WinForm_01.Properties.Resources.resources", Namespace = "Test_WinForm_01.Properties.Resources.resources" } };
     compiler.AssembliesFiles = new string[] { frameworkDir + "mscorlib.dll", frameworkDir + "System.dll", frameworkDir + "System.Windows.Forms.dll", frameworkDir + "System.Drawing.dll" };
     compiler.Compile();
 }
Beispiel #8
0
        public static void CompileProject(string projectFile)
        {
            CSharpProject project = CSharpProject.Create(projectFile);

            CSharpCompiler compiler = new CSharpCompiler();
            compiler.LanguageVersion = project.LanguageVersion;
            compiler.OutputKind = project.OutputKind;
            compiler.OptimizationLevel = project.OptimizationLevel;
            compiler.Platform = project.Platform;
            compiler.GeneralDiagnosticOption = project.GeneralDiagnosticOption;
            compiler.WarningLevel = project.WarningLevel;

            compiler.AssemblyName = project.AssemblyName;
            compiler.OutputPath = project.OutputPath;
            compiler.PdbPath = project.PdbPath;
            compiler.Win32ResourceFile = project.Win32ResourceFile;
            compiler.PreprocessorSymbols = project.PreprocessorSymbols;
            compiler.SourceFiles = project.SourceFiles;
            compiler.ResourceFiles = project.ResourceFiles;
            compiler.AssembliesFiles = project.AssembliesFiles;
            compiler.Compile();
        }
        /// <summary>
        /// Builds the expected base of touched files.
        /// Adds a hook for temporary file creation as well,
        /// so this method must be called before the execution of
        /// Csc.Run.
        /// </summary>
        private static void BuildTouchedFiles(CSharpCompiler cmd,
                                              string outputPath,
                                              out List<string> expectedReads,
                                              out List<string> expectedWrites)
        {
            expectedReads = cmd.Arguments.MetadataReferences
                .Select(r => r.Reference).ToList();

            foreach (var file in cmd.Arguments.SourceFiles)
            {
                expectedReads.Add(file.Path);
            }

            var writes = new List<string>();
            writes.Add(outputPath);

            expectedWrites = writes;
        }
Beispiel #10
0
 public void CanCompile()
 {
     CompilerSettings s = new CompilerSettings();
       CSharpCompiler c = new CSharpCompiler(s);
       Assert.True(c.CanCompileFile(@"..\..\etc\testdata\compiler\compile_ok.cs"));
 }
Beispiel #11
0
 public CustomCompilationService(CSharpCompiler compiler,
     IRazorViewEngineFileProviderAccessor fileProviderAccessor, IOptions<RazorViewEngineOptions> optionsAccessor,
     ILoggerFactory loggerFactory) : base(compiler, fileProviderAccessor, optionsAccessor, loggerFactory)
 {
 }
        /// <summary>
        /// Builds the expected base of touched files.
        /// Adds a hook for temporary file creation as well,
        /// so this method must be called before the execution of
        /// Csc.Run.
        /// </summary>
        private static void BuildTouchedFiles(CSharpCompiler cmd, 
                                              string outputPath,
                                              out List<string> expectedReads,
                                              out List<string> expectedWrites)
        {
            expectedReads = cmd.Arguments.MetadataReferences
                .Select(r => r.Reference).ToList();

            foreach (var file in cmd.Arguments.SourceFiles)
            {
                expectedReads.Add(file.Path);
            }

            var writes = new List<string>();
            writes.Add(outputPath);

            // Hook temporary file creation
            cmd.PathGetTempFileName = () =>
            {
                // Named 'GetFileName', but actually a path
                string tempPath = Path.GetTempFileName();
                writes.Add(tempPath);
                return tempPath;
            };
            expectedWrites = writes;
        }
        protected TranslationTestHelper(string asmName, string src, bool isDebug)
        {
            var compiler = new CSharpCompiler();
            var asm = Assembly.Load(asmName);
            var result = compiler.CompileSource(src, asm, isDebug);

            var dir = Path.GetDirectoryName(result.PathToAssembly);
            var compiledAsmName = Path.GetFileNameWithoutExtension(result.PathToAssembly);

            this.resolver.AddSearchDirectory(dir);

            this.typeSystem = new TypeSystem(this.resolver);
            this.CompiledAssembly = this.typeSystem.LoadAssembly(compiledAsmName);

            //this.CompiledAssembly.MainModule.LoadSymbols();
            var pathToPdb = Path.Combine(dir, compiledAsmName + ".pdb");
            File.Delete(pathToPdb);

            File.Delete(result.PathToAssembly);
        }
Beispiel #14
0
 private Action<object, object> Compile()
 {
     var compiler = new CSharpCompiler(Field);
       return compiler.Compile(_code);
 }