zoukankan      html  css  js  c++  java
  • 使用Roslyn编译项目的示例

    using System;
    using System.Collections.Generic;
    using System.IO;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.Emit;
    using Microsoft.CodeAnalysis.MSBuild;
    
    namespace Roslyn.TryItOut
    {
        class Program
        {
            static void Main(string[] args)
            {
                string solutionUrl = "C:\Dev\Roslyn.TryItOut\Roslyn.TryItOut.sln";
                string outputDir = "C:\Dev\Roslyn.TryItOut\output";
    
                if (!Directory.Exists(outputDir))
                {
                    Directory.CreateDirectory(outputDir);
                }
    
                bool success = CompileSolution(solutionUrl, outputDir);
    
                if (success)
                {
                    Console.WriteLine("Compilation completed successfully.");
                    Console.WriteLine("Output directory:");
                    Console.WriteLine(outputDir);
                }
                else
                {
                    Console.WriteLine("Compilation failed.");
                }
    
                Console.WriteLine("Press the any key to exit.");
                Console.ReadKey();
            }
    
            private static bool CompileSolution(string solutionUrl, string outputDir)
            {
                bool success = true;
    
                MSBuildWorkspace workspace = MSBuildWorkspace.Create();
                Solution solution = workspace.OpenSolutionAsync(solutionUrl).Result;
                ProjectDependencyGraph projectGraph = solution.GetProjectDependencyGraph();
                Dictionary<string, Stream> assemblies = new Dictionary<string, Stream>();
    
                foreach (ProjectId projectId in projectGraph.GetTopologicallySortedProjects())
                {
                    Compilation projectCompilation = solution.GetProject(projectId).GetCompilationAsync().Result;
                    if (null != projectCompilation && !string.IsNullOrEmpty(projectCompilation.AssemblyName))
                    {
                        using (var stream = new MemoryStream())
                        {
                            EmitResult result = projectCompilation.Emit(stream);
                            if (result.Success)
                            {
                                string fileName = string.Format("{0}.dll", projectCompilation.AssemblyName);
    
                                using (FileStream file = File.Create(outputDir + '\' + fileName))
                                {
                                    stream.Seek(0, SeekOrigin.Begin);
                                    stream.CopyTo(file);
                                }
                            }
                            else
                            {
                                success = false;
                            }
                        }
                    }
                    else
                    {
                        success = false;
                    }
                }
    
                return success;
            }
        }
    }

    实际测试的时候还需要通过nuget安装下面的包:

    Microsoft.Build、Microsoft.Build.Tasks.Core

    出处:https://stackoverflow.com/questions/13280008/how-do-i-compile-a-c-sharp-solution-with-roslyn

       http://www.cnblogs.com/VAllen/p/use-roslyn-build-project.html

  • 相关阅读:
    python 星号*使用方法
    python print 使用分隔符 或行尾符
    python 打印输出至文件中, 'wt'读写文件方式,会把原文件内容清空
    python 换行符的识别问题,Unix 和Windows 中是不一样的
    python 读不同编码的文本,传递一个可选的encoding 参数给open() 函数
    django学习笔记(4)
    dos下edit编辑器的快捷命令一览
    django学习笔记(3)
    django学习笔记(2)
    django学习笔记(1)
  • 原文地址:https://www.cnblogs.com/a14907/p/7080585.html
Copyright © 2011-2022 走看看