Skip to content

Commit 95a9822

Browse files
authored
Fix README (#23)
The README.md previously file had instructions for how to emit images to disk. Those instructions were inaccurate and it's hard to make them accurate for all runtimes. Changed it to use in memory execution of the assembly.
1 parent d0b3560 commit 95a9822

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ for `netstandard2.0`, `netcoreapp3.1` and `net5.0`. These can be easily
1616
integrated into the existing Roslyn APIs.
1717

1818
```c#
19-
using Microsoft.CodeAnalysis;
2019
using Microsoft.CodeAnalysis.CSharp;
2120
using Basic.Reference.Assemblies;
21+
using System.IO;
22+
using System.Reflection;
2223

2324
var code = @"
2425
using System;
25-
static class Program
26+
public static class Example
2627
{
27-
static void Main()
28+
public static void Main()
2829
{
2930
var tuple = (Part1: ""hello"", Part2: ""world"");
3031
Console.WriteLine($""{tuple.Part1} {tuple.Part2}"");
@@ -38,8 +39,13 @@ var compilation = CSharpCompilation
3839
new[] { CSharpSyntaxTree.ParseText(code) },
3940
references: ReferenceAssemblies.Net50);
4041

41-
using var fileStream = new FileStream(@"p:\temp\helloworld.exe", FileMode.Create, FileAccess.ReadWrite);
42-
var emitResults = compilation.Emit(fileStream);
42+
using var stream = new MemoryStream();
43+
var emitResults = compilation.Emit(stream);
44+
stream.Position = 0;
45+
var assembly = Assembly.Load(stream.ToArray());
46+
var method = assembly.GetType("Example").GetMethod("Main");
47+
method.Invoke(null, null); // Prints "Hello World"
48+
4349
```
4450

4551
This package also adds extensions methods for easily retargeting `Compilation`

0 commit comments

Comments
 (0)