从C#调用MSBuild的方法如下:
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class BuildTask : ToolTask
{
// 定义属性和参数
[Required]
public string ProjectFile { get; set; }
public string Target { get; set; }
public string Configuration { get; set; }
public string Platform { get; set; }
public bool NoLogo { get; set; }
public bool NoConsoleLogger { get; set; }
public bool Verbosity { get; set; }
public bool MaxCpuCount { get; set; }
public bool NodeReuse { get; set; }
public bool DetailedSummary { get; set; }
public bool IgnoreProjectExtensions { get; set; }
public bool ValidateProjectFile { get; set; }
public bool Restore { get; set; }
public bool ConsoleLoggerParameters { get; set; }
public bool DistributedFileLogger { get; set; }
public bool FileLogger { get; set; }
public bool FileLoggerParameters { get; set; }
public bool GraphBuild { get; set; }
public bool Interactive { get; set; }
public bool IsolateProjects { get; set; }
public bool LogCommand { get; set; }
public bool LogOutput { get; set; }
public bool LowPriority { get; set; }
public bool Maxcpucount { get; set; }
public bool MSBuildArchitecture { get; set; }
public bool MSBuildExtensionsPath { get; set; }
public bool MSBuildExtensionsPath32 { get; set; }
public bool MSBuildExtensionsPath64 { get; set; }
public bool MSBuildLoadMicrosoftTargetsReadOnly { get; set; }
public bool MSBuildNodeMode { get; set; }
public bool MSBuildSdksPath { get; set; }
public bool MSBuildToolsPath { get; set; }
public bool MSBuildToolsVersion { get; set; }
public bool MSBuildVersion { get; set; }
public bool NoAutoResponse { get; set; }
public bool NoConsoleLogger { get; set; }
public bool NoLogo { get; set; }
public bool NonInteractive { get; set; }
public bool OutputResolver { get; set; }
public bool Preprocess { get; set; }
public bool ProvideProjectDesignation { get; set; }
public bool Quiet { get; set; }
public bool Rebuild { get; set; }
public bool RestorePackages { get; set; }
public bool ShowCommandLine { get; set; }
public bool ShowConfigFile { get; set; }
public bool ShowDescription { get; set; }
public bool ShowEventId { get; set; }
public bool ShowHelp { get; set; }
public bool ShowTargetFrameworkMoniker { get; set; }
public bool ShowTimestamp { get; set; }
public bool SkipCompilerExecution { get; set; }
public bool StopOnFirstFailure { get; set; }
public bool TargetFrameworkMoniker { get; set; }
public bool TargetFrameworkVersion { get; set; }
public bool TargetPath { get; set; }
public bool Timeout { get; set; }
public bool Toolset { get; set; }
public bool Validate { get; set; }
public bool Version { get; set; }
public bool WarningLevel { get; set; }
public bool WarningsAsErrors { get; set; }
public bool WarningsNotAsErrors { get; set; }
// 重写Execute方法
protected override string GenerateCommandLineCommands()
{
CommandLineBuilder builder = new CommandLineBuilder();
builder.AppendSwitch("/t:" + Target);
builder.AppendSwitchIfNotNull("/p:Configuration=", Configuration);
builder.AppendSwitchIfNotNull("/p:Platform=", Platform);
builder.AppendSwitchIfNotNull("/nologo", NoLogo);
builder.AppendSwitchIfNotNull("/noconsolelogger", NoConsoleLogger);
builder.AppendSwitchIfNotNull("/v:", Verbosity);
builder.AppendSwitchIfNotNull("/maxcpucount:", MaxCpuCount);
builder.AppendSwitchIfNotNull("/nodeReuse:", NodeReuse);
builder.AppendSwitchIfNotNull("/detailedsummary", DetailedSummary);
builder.AppendSwitchIfNotNull("/ignoreProjectExtensions", IgnoreProjectExtensions);
builder.AppendSwitchIfNotNull("/validateProjectFile", ValidateProjectFile);
builder.AppendSwitchIfNotNull("/restore", Restore);
builder.AppendSwitchIfNotNull("/consoleLoggerParameters", ConsoleLoggerParameters);
builder.AppendSwitchIfNotNull("/distributedFileLogger", DistributedFileLogger);
builder.AppendSwitchIfNotNull("/fileLogger", FileLogger);
builder.AppendSwitchIfNotNull("/fileLoggerParameters", FileLoggerParameters);
builder.AppendSwitchIfNotNull("/graphBuild", GraphBuild);
builder.AppendSwitchIfNotNull("/interactive", Interactive);
builder.AppendSwitchIfNotNull("/isolateProjects", IsolateProjects);
builder.AppendSwitchIfNotNull("/logCommand", LogCommand);
builder.AppendSwitchIfNotNull("/logOutput", LogOutput);
builder.AppendSwitchIfNotNull("/lowPriority", LowPriority);
builder.AppendSwitchIfNotNull("/maxcpucount", MaxCpuCount);
builder.AppendSwitchIfNotNull("/MSBuildArchitecture", MSBuildArchitecture);
builder.AppendSwitchIfNotNull("/MSBuildExtensionsPath", MSBuildExtensionsPath);
builder.AppendSwitchIfNotNull("/MSBuildExtensionsPath32", MSBuildExtensionsPath32);
builder.AppendSwitchIfNotNull("/MSBuildExtensionsPath64", MSBuildExtensionsPath64);
builder.AppendSwitchIfNotNull("/MSBuildLoadMicrosoftTargetsReadOnly", MSBuildLoadMicrosoftTargetsReadOnly);
builder.AppendSwitchIfNotNull("/MSBuildNodeMode
领取专属 10元无门槛券
手把手带您无忧上云