Skip to content

Instantly share code, notes, and snippets.

@rushfrisby
Created August 3, 2014 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rushfrisby/7a0ceb8ffa4098e65d50 to your computer and use it in GitHub Desktop.
Save rushfrisby/7a0ceb8ffa4098e65d50 to your computer and use it in GitHub Desktop.
PCL breaks in roslyn
using AutoMapper;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.IO;
using System.Text;
namespace PclBreakage
{
class Program
{
static void Main()
{
var references = new[]
{
new MetadataFileReference(typeof (object).Assembly.Location),
new MetadataFileReference(typeof(Mapper).Assembly.Location)
};
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var sb = new StringBuilder();
sb.Append("using AutoMapper;");
sb.Append("public class MyClass {");
sb.Append("public MyClass() {");
sb.Append("Mapper.CreateMap<MyClass, MyClass>();");
sb.Append("}");
sb.Append("}");
var compilation = CSharpCompilation.Create("compiled.dll", new[] { SyntaxFactory.ParseSyntaxTree(sb.ToString()) }, references, options);
byte[] result;
using (var stream = new MemoryStream())
{
var emitResult = compilation.Emit(stream);
if (emitResult.Success)
{
result = stream.ToArray();
}
else
{
throw new Exception("compile failed");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment