Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / Win32

IlPad

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
21 Sep 2012CPOL2 min read 28.9K   772   15   5
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer http://www.icemanind.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Paul Tait21-Sep-12 20:19
Paul Tait21-Sep-12 20:19 
GeneralRe: My vote of 2 Pin
icemanind22-Sep-12 3:18
icemanind22-Sep-12 3:18 
GeneralRe: My vote of 2 Pin
Winfried Ripken22-Sep-12 3:58
Winfried Ripken22-Sep-12 3:58 
I´m shure ther´re many programs, wich can do this, but that´s no reason to give the article a 2! See it as an explanation on HOW to do it .. 4 stars from me

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.