Пример #1
0
        private void Compile(CodeDom.CodeDomProvider provider)
        {
            const string source =
                "public class Sample5Mem {public void Do(){System.Console.WriteLine(\"Hello World\");System.Console.ReadLine();} }";

            var param = new CodeDom.CompilerParameters
                {
                    GenerateExecutable = false,
                    IncludeDebugInformation = false,
                    GenerateInMemory = true
                };
            CodeDom.CompilerResults cr = provider.CompileAssemblyFromSource(param, source);
            object instance = cr.CompiledAssembly.CreateInstance("Sample5Mem");
            if (instance != null)
            {
                Type type = instance.GetType();
                type.InvokeMember("Do",
                                  BindingFlags.InvokeMethod | BindingFlags.Default,
                                  null,
                                  instance,
                                  null);
            }
        }
        private static Assembly Compile(string migration)
        {
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(migration);
            string assemblyName = Path.GetRandomFileName();
            MetadataReference[] references = new[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location), // System
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), // System.Linq
                MetadataReference.CreateFromFile(typeof(GeneratedCodeAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(DbType).Assembly.Location), // System.Data
                MetadataReference.CreateFromFile(typeof(ExportAttribute).Assembly.Location), // System.ComponentModel.Composition
                MetadataReference.CreateFromFile(typeof(IDatabase).Assembly.Location), // MigSharp
            };
            CSharpCompilation compilation = CSharpCompilation.Create(assemblyName, new[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                List<string> compilationErrors = new List<string>();
                foreach (Diagnostic diagnostic in result.Diagnostics.Where(diagnostic =>
                    diagnostic.IsWarningAsError ||
                    diagnostic.Severity == DiagnosticSeverity.Error))
                {
                    compilationErrors.Add(diagnostic.Id + ": " + diagnostic.GetMessage());
                }
                CollectionAssert.IsEmpty(compilationErrors, "Could not compile migration."); // this nicely outputs the compilation errors at the top of the output
                Assert.IsTrue(result.Success, "Could not compile migration.");
                ms.Seek(0, SeekOrigin.Begin);
                return Assembly.Load(ms.ToArray());
            }
        }