var source = @"using System; namespace MyNamespace { public class MyClass { public void MyMethod() { Console.WriteLine(""Hello world!""); } } }"; var projectContent = new CSharpProjectContent(); projectContent.AddOrUpdateFile("MyFile.cs", source); var compilation = projectContent.CreateCompilation();
var projectContent = new CSharpProjectContent(); projectContent.MetadataReferences.Add(MetadataReference.CreateFromFile("mscorlib.dll")); projectContent.MetadataReferences.Add(MetadataReference.CreateFromFile("System.dll")); var compilation = projectContent.CreateCompilation();In this example, we are creating a compilation with two metadata references: `mscorlib.dll` and `System.dll`. We first create a new instance of `CSharpProjectContent` and add the references to its `MetadataReferences` collection. Finally, we create a compilation by calling the `CreateCompilation` method on the project content object. Package library: `Microsoft.CodeAnalysis` (can be found in the `Microsoft.CodeAnalysis.Common` package or `Microsoft.CodeAnalysis.CSharp` package for C# specific features)