使用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

posted @ 2017-06-26 14:52  a14907  阅读(1041)  评论(0)    收藏  举报