public ComplierError GenerateModuleAssembly(IList <BusinessObject> bos) { string code; SyntaxTree tree = GetSyntaxTree(bos, out code); //var cw = MSBuildWorkspace.Create(); //OptionSet options = cw.Options; //options = options.WithChangedOption(CSharpFormattingOptions.OpenBracesInNewLineForMethods, false); //options = options.WithChangedOption(CSharpFormattingOptions.OpenBracesInNewLineForTypes, false); //SyntaxNode formattedNode = Formatter.Format( // tree.GetRoot(), // cw, // options // ); //code = formattedNode.ToString(); var refs = CollectReferencedAssemblies(); var compilation = CSharpCompilation.Create("IMatrixRuntimeBusinessObject.dll", new[] { tree }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), references: refs); var outputPath = AdmiralEnvironment.UserDefineBusinessTempFile.FullName; var rst = compilation.Emit(outputPath); if (rst.Success) { return(null); } else { var obj = new ComplierError(string.Join("\n", rst.Diagnostics.Select(x => x.ToString()))); obj.Code = tree.GetText().ToString(); return(obj); } //Assembly compiledAssembly; //using (var stream = new MemoryStream()) //{ // compiledAssembly = Assembly.Load(stream.GetBuffer()); //} //var calculatorClass = compiledAssembly.GetType("Calculator"); //var evaluateMethod = calculatorClass.GetMethod("Evaluate"); //var result = evaluateMethod.Invoke(null, null).ToString(); //Console.WriteLine(result); }
public ComplierError GenerateModuleAssembly(IList<BusinessObject> bos) { string code; SyntaxTree tree = GetSyntaxTree(bos,out code); //var cw = MSBuildWorkspace.Create(); //OptionSet options = cw.Options; //options = options.WithChangedOption(CSharpFormattingOptions.OpenBracesInNewLineForMethods, false); //options = options.WithChangedOption(CSharpFormattingOptions.OpenBracesInNewLineForTypes, false); //SyntaxNode formattedNode = Formatter.Format( // tree.GetRoot(), // cw, // options // ); //code = formattedNode.ToString(); var refs = CollectReferencedAssemblies(); var compilation = CSharpCompilation.Create("IMatrixRuntimeBusinessObject.dll", new[] { tree }, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary), references: refs); var outputPath = AdmiralEnvironment.UserDefineBusinessTempFile.FullName; var rst = compilation.Emit(outputPath); if (rst.Success) return null; else { var obj = new ComplierError(string.Join("\n", rst.Diagnostics.Select(x => x.ToString()))); obj.Code = tree.GetText().ToString(); return obj; } //Assembly compiledAssembly; //using (var stream = new MemoryStream()) //{ // compiledAssembly = Assembly.Load(stream.GetBuffer()); //} //var calculatorClass = compiledAssembly.GetType("Calculator"); //var evaluateMethod = calculatorClass.GetMethod("Evaluate"); //var result = evaluateMethod.Invoke(null, null).ToString(); //Console.WriteLine(result); }