Untitled diff

Created Diff never expires
6 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
52 lines
16 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
60 lines
using System;
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Linq;
using System.Reflection;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp;


class Program
class Program
{
{
static void Main(string[] args)
static void Main(string[] args)
{
{
var greetings = HelloWorlder.GetGreetings();
var greetings = HelloWorlder.GetGreetings();
// should write Hello World
// should write Hello World
greetings();
greetings();
}
}
}
}


public static class HelloWorlder
public static class HelloWorlder
{
{
public static Action GetGreetings()
public static Action GetGreetings()
{
{
var tree = CSharpSyntaxTree.ParseText(@"
var tree = CSharpSyntaxTree.ParseText(@"
using System;
using System;
public class MyClass
public class MyClass
{
{
public static void Main()
public static void Main()
{
{
Console.WriteLine(""Hello World!"");
//Console.WriteLine(""Hello World!"");
Console.ReadLine();
//Console.ReadLine();
}
}
}");
}");
const string testAsmName = "testLib";
const string testAsmName = "testLib";


var coreDir = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);
var coreDir = Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location);


var mscorlib = MetadataReference.CreateFromFile(Path.Combine(coreDir, "mscorlib.dll"));
var mscorlib = MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Private.CoreLib.dll"));

var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create(testAsmName,
var compilation = CSharpCompilation.Create(testAsmName,
syntaxTrees: new[] { tree }, references: new[] { mscorlib });
syntaxTrees: new[] { tree }, references: new[] { mscorlib }, options: options);


var emitResult = compilation.Emit($"{testAsmName}.dll");
var ms = new MemoryStream();

var emitResult = compilation.Emit(ms);
ms.Seek(0, SeekOrigin.Begin);


if (!emitResult.Success)
if (!emitResult.Success)
{
{
throw new Exception(string.Join(Environment.NewLine, emitResult.Diagnostics.Select((x, i) => $"{i + 1}. {x}")));
throw new Exception(string.Join(Environment.NewLine, emitResult.Diagnostics.Select((x, i) => $"{i + 1}. {x}")));
}
}
var ourAssembly = Assembly.Load(new AssemblyName(testAsmName));

var ourAssembly = Assembly.Load(ms.ToArray());
var type = ourAssembly.GetType("MyClass");
var type = ourAssembly.GetType("MyClass");


var meth = type.GetRuntimeMethod("Main", Type.EmptyTypes);
var meth = type.GetRuntimeMethod("Main", Type.EmptyTypes);

ms.Close();
return () => meth.Invoke(null, null);
return () => meth.Invoke(null, null);
}
}
}
}