How add existing project to c# solution with roslyn?

心已入冬 提交于 2019-12-08 13:44:51

问题


I want to add an existing project to my c# solution.I used TryApplyChanges and it returns true but it doesn't save changes to real Solution. I used Microsoft.CodeAnalysis.* version 1.1.1.

 public void AddProject (string solutionName)
 {
    MSBuildWorkspace ws=MSBuildWorkspace.Create ();
    ws.OpenSolutionAsync (solutionName);
    ws.OpenProjectAsync ("ProjectName");
    if (ws.TryApplyChanges  (ws.CurrentSolution )
    {// break point is here 
    }
 } 

回答1:


There's a few problems with your code:

  1. MSBuildWorkspace doesn't (currently) support adding/or removing projects from the workspace and saving them back to the solution file. We're open source if you want to look into improving this.
  2. Calling OpenProjectAsync isn't really expected to modify the solution as you're expecting to do so...it just populates the workspace with the information.
  3. You're not awaiting the Async calls, so even then they might have not finished their work before marching on.

The first is obviously the most critical for you, but the others should be noted too.



来源:https://stackoverflow.com/questions/36135604/how-add-existing-project-to-c-sharp-solution-with-roslyn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!