Roslyn AddDocument to the project and save this change in a real solution file

This should add a document.

public static void AddDoc() { var msBuild = MSBuildWorkspace.Create(); var sln = msBuild.OpenSolutionAsync (@"D:\PanNiebieski\Documents\Visual Studio 14\Projects\WebApplication1" + @"\WebApplication1.sln").Result; foreach (var p in sln.Projects) { p.AddDocument(Guid.NewGuid().ToString() + ".txt", "test"); var ok = msBuild.TryApplyChanges(sln); Console.WriteLine(p.Name + ":" + ok); } Console.ReadKey(); } 

The TryApplyChanges method returns true so that the document is added. Again, when I check the solution, nothing of the sort exists. I have the same problem with adding project links.

The question is how can I save the changes, for example, adding a document to this project. I'm missing something. Many of the questions in StackOverflow about adding project links say this just doesn't work. Does the AddDocument method work?

This method says that this action is supported. I am embarrassed.

enter image description here

+6
source share
2 answers

The Roslyn APIs for the entire workspace and syntax are immutable.

p.AddDocument creates a new Project and Solution (which is returned in the Project property of the returned Document ), which you ignore.

+8
source

This needs to be done:

 IWorkspace workspace = Workspace.LoadSolution(@"..\RoslynTest.sln"); var originalSolution = workspace.CurrentSolution; var project = originalSolution.GetProject(originalSolution.ProjectIds.First()); IDocument doc = project.AddDocument("index.html", "<html></html>"); workspace.ApplyChanges(originalSolution, doc.Project.Solution); 

Source: http://www.wenda.io/questions/982766/roslyn-add-a-document-to-a-project.html

UPDATE: it is no longer applicable.

+5
source

Source: https://habr.com/ru/post/981143/


All Articles