问题
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:
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.- 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.
- 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