/// <summary> /// Attempts to compile the specified C# source files. /// </summary> /// <param name="sourceFiles">An array of filenames to batch compile</param> /// <param name="extraReferences">Any additional assembly reference names</param> /// <returns>True if the compile was successful or false if there were one or more errors</returns> public bool CompileFiles(string[] sourceFiles, params string[] extraReferences) { // Set the compiling flag isCompiling = true; ResetCompiler(); ScriptCompilerError[] compileResult = null; lock (compilerLock) { // Register references compiler.AddReferences(extraReferences); // Invoke the compiler compileResult = compiler.CompileFiles(sourceFiles); // Get assembly data assemblyData = compiler.AssemblyData; symbolsData = compiler.SymbolsData; } // Success flag bool successful = true; foreach (ScriptCompilerError err in compileResult) { if (err.isWarning == true) { // Generate a warning AddWarning(err.errorCode, err.errorText, err.fileName, err.line, err.column); } else { // Set flag successful = false; // Generate an error AddError(err.errorCode, err.errorText, err.fileName, err.line, err.column); } } // Check for success if (successful == false) { assemblyData = null; symbolsData = null; } // Set the compiling flag isCompiling = false; // Check for success return(successful); }
public void AddsAListOfReferencesByTypeName() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem4"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); Assert.IsType <Compiler>(c); }
public void CatchesErrors() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem11"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); c.Source = badSource; c.Compile(); Assert.True(c.HasErrors); Assert.Null(c.Assembly); Assert.Equal(2, c.Diagnostics.Count()); }
public void EmitsAssembly() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem9"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); c.Source = source; c.Compile(); Assert.NotNull(c.Assembly); Assert.Equal("Joe", c.Assembly.DefinedTypes.First().Name); Assert.Equal("Fred", c.Assembly.ExportedTypes.First().Namespace); }
public void GeneratesCompilationWithReferences() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem8"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); c.Source = source; c.Compile(); var cp = c.Compilation; Assert.NotNull(cp); Assert.Equal("Assem8", cp.AssemblyName); Assert.Equal(3, cp.References.Count()); }
public void CanUseAssemblyFromSyntaxTree() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem13"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); c.SyntaxTree = CreateSyntaxTree(); c.Compile(); Type type = c.Assembly.ExportedTypes.Where(t => t.Name == "Henry").FirstOrDefault(); var joe = Activator.CreateInstance(type); Assert.IsType(type, joe); var getNextInt = (Func <int>)Delegate .CreateDelegate(typeof(Func <int>), joe, type.GetMethod("GetNextInt")); Assert.Equal(13, getNextInt()); Assert.Equal(14, getNextInt()); }
public void CanInvokeEmittedMethods() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem10"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); c.Source = source; c.Compile(); Type type = c.Assembly.ExportedTypes.Where(t => t.Name == "Joe").FirstOrDefault(); var joe = Activator.CreateInstance(type); Assert.IsType(type, joe); var getNextInt = (Func <int>)Delegate .CreateDelegate(typeof(Func <int>), joe, type.GetMethod("GetNextInt")); Assert.Equal(1, getNextInt()); Assert.Equal(2, getNextInt()); }
public void CanUseAssemblyFromSourceFile() { ICompiler c = Compiler.Create(); c.AssemblyName = "Assem12"; c.AddReferences(new string[] { "System.Int32", "System.Double", "System.IO.Path" }); string tmpFile = Path.Combine(Path.GetTempPath(), "DynamicCSharpTest.cs"); using (var outFile = new StreamWriter(tmpFile, false)) outFile.Write(source); c.Source = null; using (c.SourceStream = new FileStream(tmpFile, FileMode.Open, FileAccess.Read)) c.Compile(); File.Delete(tmpFile); Type type = c.Assembly.ExportedTypes.Where(t => t.Name == "Joe").FirstOrDefault(); var joe = Activator.CreateInstance(type); Assert.IsType(type, joe); var getNextInt = (Func <int>)Delegate .CreateDelegate(typeof(Func <int>), joe, type.GetMethod("GetNextInt")); Assert.Equal(1, getNextInt()); Assert.Equal(2, getNextInt()); }
/// <summary> /// Attempts to compile the specified C# source content. /// </summary> /// <param name="sourceContent">The string source content array containing the C# source code</param> /// <param name="extraReferences">Any additional assembly reference names</param> /// <returns>True if the compile was successful or false if there were one or more errors</returns> public bool CompileSources(string[] sourceContent, params string[] extraReferences) { // Set the compiling flag isCompiling = true; // Reset result assemblyData = null; // Reset errors errors = new string[0]; warnings = new string[0]; assemblyData = null; byte[] assemblyResult = null; ScriptCompilerError[] compileResult = null; lock (compilerLock) { // Register references compiler.AddReferences(extraReferences); // Invoke the compiler compileResult = compiler.Compile(sourceContent); // Get assembly data assemblyResult = compiler.AssemblyData; } // Success flag bool successful = true; foreach (ScriptCompilerError err in compileResult) { if (err.isWarning == true) { // Generate a warning AddWarning(err.errorCode, err.errorText, err.fileName, err.line, err.column); } else { // Set flag successful = false; errorLineValue = err.line; if (err.line != 0) { UnityEngine.Debug.Log("eeeeeeee" + err.line); UnityEngine.GameObject.Find("ErrorShowLine").GetComponent <UnityEngine.RectTransform>().localPosition = new UnityEngine.Vector3(0, 481f - (16f * (err.line - 2)), 0); } // Generate an error AddError(err.errorCode, err.errorText, err.fileName, err.line, err.column); } } // Check for success if (successful == true) { assemblyData = assemblyResult; } // Set the compiling flag isCompiling = false; // Check for success return(successful); }