Roslyn Add a document to a project

前端 未结 2 1398
夕颜
夕颜 2020-12-15 11:50

I\'m running roslyn ctp2

I am attempting to add a new html file to a project

IWorkspace workspace = Workspace.LoadSolution(\"MySolution.sln\");
var o         


        
2条回答
  •  生来不讨喜
    2020-12-15 12:27

    Workspaces are immutable. That means that any method that sounds like it's going to modify the workspace will instead be returning a new instance with the changes applied.

    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", "");
    workspace.ApplyChanges(originalSolution, doc.Project.Solution);
    

    However, I'm not near a machine with Roslyn installed at the moment, so I can't guarantee this 100%.

提交回复
热议问题