Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IlPad

0.00/5 (No votes)
21 Sep 2012 1  
A program to compile C# code into MSIL code

Image 1

Introduction

A while ago, I was trying to improve the efficiency of a program and in doing so, I needed to view the MSIL of the assembly I was working on. It soon became very tedious to have to build my program then switch over to Microsoft Ildasm to have to keep looking at my disassembly. I tried looking on the Internet for something that would let me view my C# code and my IL code together, but I found no such thing. Hence, IlPad was born.

What It Does

IlPad opens up with 4 panels. The top panel is a text box where you can enter C# code. It can be a whole program or just part of a class. To the right is another panel which shows which assemblies you currently have referenced. There is a button that opens up where you can reference even more assemblies if you need to. Below the C# editor is a button. Clicking on this button will compile your snippet and the resulting IL code is shown in another text box below that. If there are any errors in your C# code, the errors will be outputted to a list box at the bottom of IlPad. The list box will give you a description of the error along with a line number where the error occurred. IlPad is that easy to use!

In addition to all that, I have included two sample snippet codes. By clicking on either of the two snippet code buttons at the top of the window, the text box will automatically fill in a code snippet. Both of these snippets run without having to reference any additional assemblies.

How IlPad Works

IlPad works by taking advantage of the CSharpCodeProvider class. This class will let you dynamically compile C# code into either an in-memory assembly or an assembly outputted to a file. Here is a snippet of code from IlPad that does the compilation:

var options = new Dictionary<string, string> {{"CompilerVersion", "v4.0"}};
var provider = new CSharpCodeProvider(options);
var parms = new CompilerParameters
    {
        CompilerOptions = @"/lib:C:\Windows\assembly\GAC /target:library",
        GenerateExecutable = true,
        GenerateInMemory = false
    };
parms.ReferencedAssemblies.Add("System.dll");
foreach (string item in lbReferences.Items)
{
    parms.ReferencedAssemblies.Add(item + ".dll");
}
string outFile = Path.GetTempFileName();
string ilOutFile = Path.GetTempFileName();
string ildasm = Path.GetTempFileName() + ".exe";
parms.OutputAssembly = outFile;
CompilerResults res = provider.CompileAssemblyFromSource(parms, txtInputProgram.Text);

IlPad targets .NET 4.0. IlPad could be made to run in .NET 3.5, if needed. IlPad creates a temporary file to store the compiled assembly. Once the assembly is successfully created, IlPad runs IlDasm. IlDasm is a disassembler created by Microsoft that disassembles .NET assemblies into MSIL code.

Things Yet to Do

When I get more time, there are some things I'd like to do to improve IlPad. Here is a short to do list:

  • Add VB.NET support
  • Improve the "Add a Reference" dialog
  • Syntax highlighting (both source code and IL code)
  • Automatic code formatting
  • Possibly add F# support
  • Create a snippet library and add more snippets
  • Ability to create and store your own snippets in the snippet library

Of course, if anyone has any suggestions or things they'd like to see added, I'd love to hear any ideas!

History

  • 1.0 - Initial release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here