Roslyn Add document to project

I run roslyn ctp2

I am trying to add a new html file to the project

IWorkspace workspace = Workspace.LoadSolution("MySolution.sln"); var originalSolution = workspace.CurrentSolution; ISolution newSolution = originalSolution; newSolution.GetProject(newSolution.ProjectIds.First()) .AddDocument("index.html", "<html></html>"); workspace.ApplyChanges(originalSolution, newSolution); 

This leads to the fact that the changes are not recorded. I am trying to get a new html file to display in VS

+8
c # roslyn
source share
2 answers

There are two problems here:

  • Roslyn ISolution , IProject and IDocument immutable, so to see the changes, you will need to create a new ISolution with the changes, and then call Workspace.ApplyChanges() .
  • In Roslyn, IDocument objects are created only for files that are passed to the compiler. Another way of saying this is things that are part of the Compile ItemGroup in the project file. For other files (including html files) you should use the usual Visual Studio interfaces, for example IVsSolution .
+6
source share

Workspaces are immutable. This means that any method that sounds as if it is going to change the workspace will instead return a new instance with the changes made.

So you want something like:

 IWorkspace workspace = Workspace.LoadSolution("MySolution.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); 

However, I am not near the Roslyn machine currently installed, so I cannot guarantee it is 100%.

+4
source share

All Articles