我想通过使用以编程方式编译目标项目
Microsoft.Build.Evaluation.Project.Build()
如果我需要编译的目标项目包含C#
6.0 功能,例如安全导航运算符 ( ?.
Operator),它会在这些代码行中准确给出错误。
如果我在目标项目中删除这些代码行,它编译得很好。
有谁知道如何以编程方式编译C#
6.0 和 7.0 样式代码?
在构建我的运行时版本的项目中,构建版本Microsoft.Build.dll
是 v4.0.30319,.Net
框架版本是 4.6.2。
我添加了两个简单的项目。第一个控制台项目是编译的代码。第二个控制台项目是要编译的目标项目。通过注释掉第 15 行,编译成功,否则日志文件在该行显示错误。
编译的项目
using System;
using Microsoft.Build.Logging;
namespace CompilerApp
{
class Program
{
static void Main(string[] args)
{
string projectfile = @"C:\temp\CompilerApp\MyCSharp7\MyCSharp7.csproj";
UnloadAnyProject();
Microsoft.Build.Evaluation.Project p = new Microsoft.Build.Evaluation.Project(projectfile);
FileLogger loggerfile2 = new FileLogger();
loggerfile2.Parameters = @"logfile=C:\temp\CompilerApp\myapp.msbuild.log";
bool buildresult = p.Build(loggerfile2);
if (buildresult)
{
Console.WriteLine("project compiled");
}
else
{
Console.WriteLine("project not compiled, check {0}", @"C:\temp\myapp.msbuild.log");
}
p.Save();
UnloadAnyProject();
}
private static void UnloadAnyProject()
{
Microsoft.Build.Evaluation.ProjectCollection projcoll = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
foreach (Microsoft.Build.Evaluation.Project pr in projcoll.LoadedProjects)
{
Microsoft.Build.Evaluation.ProjectCollection mypcollection = pr.ProjectCollection;
mypcollection.UnloadProject(pr);
}
}
}
}
应该编译的目标项目:
using System;
namespace MyCSharp7
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("truncate {0}", Truncate("Hello", 3));
}
public static string Truncate(string value, int length)
{
//csharp 6/7
//comment out line below for succesfull compilation
return value?.Substring(0, Math.Min(value.Length, length));
// csharp<6
string result = value;
if (value != null) // Skip empty string check for elucidation
{
result = value.Substring(0, Math.Min(value.Length, length));
}
return result;
}
}
}