/// <summary> /// Adds the specified code. /// </summary> /// <param name="Code">The code.</param> /// <param name="Usings">The usings.</param> /// <param name="References">The references.</param> /// <returns>The list of types that have been added</returns> /// <exception cref="System.Exception">Any errors that are sent back by Roslyn</exception> protected IEnumerable <Type> Add(string Code, IEnumerable <string> Usings, params Assembly[] References) { if (AssemblyStream == null) { return(null); } CSharpCompilation CSharpCompiler = CSharpCompilation.Create(AssemblyName + ".dll", new SyntaxTree[] { CSharpSyntaxTree.ParseText(Code) }, References.ForEach(x => MetadataReference.CreateFromFile(x.Location)), new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: Usings, optimizationLevel: Optimize ? OptimizationLevel.Release : OptimizationLevel.Debug)); using (MemoryStream TempStream = new MemoryStream()) { EmitResult Result = CSharpCompiler.Emit(TempStream); if (!Result.Success) { throw new Exception(Code + System.Environment.NewLine + System.Environment.NewLine + Result.Diagnostics.ToString(x => x.GetMessage() + " : " + x.Location.GetLineSpan().StartLinePosition.Line, System.Environment.NewLine)); } byte[] MiniAssembly = TempStream.ToArray(); Classes.AddIfUnique((x, y) => x.FullName == y.FullName, AppDomain.CurrentDomain.Load(MiniAssembly).GetTypes()); AssemblyStream.Write(MiniAssembly, 0, MiniAssembly.Length); } return(Classes); }