Skip to content

Instantly share code, notes, and snippets.

@m0sa
Last active August 29, 2015 14:10
Show Gist options
  • Save m0sa/d0f5eab1eed43952623f to your computer and use it in GitHub Desktop.
Save m0sa/d0f5eab1eed43952623f to your computer and use it in GitHub Desktop.
Roslyn vs CodeDom IL
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.IO.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Reflection.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Text.Encoding.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Threading.Tasks.dll</Reference>
<NuGetReference Prerelease="true">Microsoft.CodeAnalysis</NuGetReference>
<NuGetReference Prerelease="true">Microsoft.CodeAnalysis.Common</NuGetReference>
<NuGetReference Prerelease="true">Microsoft.CodeAnalysis.CSharp</NuGetReference>
<NuGetReference>protobuf-net</NuGetReference>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
// TODO modify this accordingly:
private const string ildasmPath = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe";
void Main()
{
var source =
@"
public static class Program
{
public static void Main()
{
using (var ms = new System.IO.MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, new {a = 1});
}
}
}
";
var protoBufAssemblyLocation = typeof(ProtoBuf.Serializer).Assembly.Location;
var systemXmlAssemblyLocation = typeof(System.Xml.XmlAttribute).Assembly.Location;
using(var codeDom = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp")) {
var codeDomResult = codeDom.CompileAssemblyFromSource(new System.CodeDom.Compiler.CompilerParameters() { ReferencedAssemblies = { systemXmlAssemblyLocation, protoBufAssemblyLocation } }, source);
Ildasm(codeDomResult.CompiledAssembly.Location, codeDomResult.Errors, "CodeDom");
}
var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), ".dll");
Microsoft.CodeAnalysis.Emit.EmitResult emitResult;
using(var fs = File.OpenWrite(assemblyPath))
{
emitResult = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(
Path.GetFileNameWithoutExtension(assemblyPath),
new [] { Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ParseSyntaxTree(source) },
new Microsoft.CodeAnalysis.MetadataReference[] {
Microsoft.CodeAnalysis.MetadataReference.CreateFromAssembly(typeof(object).Assembly),
Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(systemXmlAssemblyLocation),
Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(protoBufAssemblyLocation)
},
new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(
Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary,
assemblyIdentityComparer: Microsoft.CodeAnalysis.DesktopAssemblyIdentityComparer.Default))
.Emit(fs);
}
Ildasm(assemblyPath, emitResult.Diagnostics, "Roslyn");
}
private void Ildasm(string assemblyLocation, object diagnostics, string description)
{
var startInfo = new ProcessStartInfo(ildasmPath, string.Format("\"{0}\" /nobar /text", assemblyLocation))
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
};
var process = Process.Start(startInfo);
var stdOut = new StringBuilder();
process.OutputDataReceived += (s, e) => { lock (stdOut) if(e.Data != null) { stdOut.AppendLine(e.Data); } };
process.BeginOutputReadLine();
process.WaitForExit();
new { assemblyLocation, diagnostics, ilDump = stdOut.ToString() }.Dump(description, 0);
try { File.Delete(assemblyLocation); } catch {};
}
// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment