Created
August 3, 2014 15:49
-
-
Save rushfrisby/7a0ceb8ffa4098e65d50 to your computer and use it in GitHub Desktop.
PCL breaks in roslyn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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