Build project with Microsoft.Build API

后端 未结 3 1443
别跟我提以往
别跟我提以往 2020-12-16 18:13

I\'m trying to build a project using the classes in Microsoft.Build.

The code is:

var project = new ProjectInstance(CS_PROJ_FILE);
project.Build();
<         


        
3条回答
  •  孤城傲影
    2020-12-16 18:41

    There is apparently a new way to fix this problem, as described here and here.

    Here's what worked for me:

    1. I deleted Microsoft.Build.dll, Microsoft.Build.Framework.dll, Microsoft.Build.Tasks.Core.dll and Microsoft.Build.Utilities.Core.dll (i.e., all Microsoft.Build .dll files) from my project's output folder.

    2. I deleted several Microsoft.Build NuGet packages from my project's references.

    3. I installed the Microsoft.Build.Locator NuGet package for my project.

    4. I added the following code to my program:

       // This needed after upgrading to Roslyn revision 34025, see these two links:
       //  https://github.com/dotnet/roslyn/issues/26029
       //  https://stackoverflow.com/a/49886334/253938
       MSBuildLocator.RegisterDefaults();
      

    This fixed the exception listed by the OP and avoided the tons of compiler error messages about being unable to resolve all references.

    Edit: More information here: https://gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3

提交回复
热议问题