var sourceCode = "public class Example {}"; var projectName = "ExampleProject"; var document = Microsoft.CodeAnalysis.CodeFactory.CreateSyntaxTree(sourceCode).ToString(); var textDocument = Microsoft.CodeAnalysis.Text.SourceText.From(document, Encoding.UTF8); var solution = new Microsoft.CodeAnalysis.AdhocWorkspace() .CurrentSolution .AddProject(projectName, projectName, "C#") .AddDocument(projectName + "/Example.cs", textDocument);
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; var sourceCode = "public class Example {}"; var syntaxTree = SyntaxFactory.ParseSyntaxTree(sourceCode); var document = new Microsoft.CodeAnalysis.Document.WithText("Example.cs", sourceCode);In this example, we also create a new document with the text "public class Example {}", but this time we use the ParseSyntaxTree method from the CSharp namespace to create the syntax tree for the code, and then pass that syntax tree and document file name to the WithText method. Overall, Document WithText is a useful class in the Microsoft.CodeAnalysis library for creating new documents with specified text, syntax trees, and file names.