简体   繁体   English

以编程方式编译/构建C#项目

[英]Compiling/Building C# Project Programmatically

I have a project in c# that I would like to compile programmatically and put it in Release folder. 我在C#中有一个项目,希望以编程方式进行编译并将其放入Release文件夹中。

I am using CodeDomProvider, here below is my code: 我正在使用CodeDomProvider,下面是我的代码:

 class Compiler
    {
      public static void Build(string filename,string outputAssemblyPath)
        {

            CodeDomProvider cdp = CodeDomProvider.CreateProvider("CSharp");
            CompilerParameters cp = new CompilerParameters()
            {
                GenerateExecutable = true,
                GenerateInMemory = true,
                IncludeDebugInformation = true,
                OutputAssembly = outputAssemblyPath
            };
            CompilerResults result = cdp.CompileAssemblyFromFile(cp, filename);

            foreach (string s in result.Output)
            {
                Console.WriteLine(s);
            }
        }
}

I am calling Build method the following way: 我通过以下方式调用Build方法:

 class Program
    {
        static void Main(string[] args)
        {
            Compiler.Build("C:/Projects/Project1/alan.project/Project1.csproj", "C:/Projects/Project1/alan.project/bin/Release/");
            Console.ReadLine();
        }
    }

All I want to do is instead of clicking "Rebuild" and have my project compiled in the Release directory, I want it done programmatically. 我要做的就是不要单击“重新生成”,而将我的项目编译在Release目录中,而是希望以编程方式完成。

One of decision it's using MSBuild SDK , that allows create custom task on C# . 使用MSBuild SDK的决定之一是允许在C#上创建自定义任务。 Through it you can controll build process, describe own logic. 通过它您可以控制构建过程,描述自己的逻辑。


using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Linq;

namespace MSBuild.Common
{
    class Build : Task
    {
        [Required]
        public string Solution
        {
            get;
            set;
        }

        public override bool Execute()
        {
            return BuildEngine2.BuildProjectFilesInParallel(SolutionFile.Parse(Solution).ProjectsByGuid.Select(p => p.Value.AbsolutePath).ToArray(), null, null, null, null, true, false);
        }
    }
}

This is task example. 这是任务示例。 It's accept path to the solution and perform parallel compile its projects. 它接受解决方案的路径并执行其项目的并行编译。 You can add properties to BuildProjectFilesInParallel that will describe build process. 您可以向BuildProjectFilesInParallel添加将描述构建过程的属性。


Call of created task will be from the custom target: 从自定义目标调用创建的任务:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="EntryBuild">

    <UsingTask AssemblyFile="Build.dll" TaskName="Build" />

    <Target Name="EntryBuild">
        <Build Solution="$(Solution)" />
    </Target>

</Project>

Execute it through MSBuild CLI : 通过MSBuild CLI执行它:

msbuild.exe /t:Build.targets /p:Solutin=%pathToSolution%

MSBuild Targets | MSBuild目标 | Task Writing 任务写作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM