ILSpy: .NET Reflector Alternatives Redux

In my last post I discussed several possible alternatives to the soon-to-be-a-paid-app .NET Reflector.  At the time, none of the options were entirely viable replacements, although ILSpy in particular showed great promise.

As ILSpy was in active development at the time, and was less than a month old, I thought it might be worth checking back in on that project.  As it turns out, I didn’t need to wait this long, as Milestone 1 of ILSpy dropped a day after my post.

Now three weeks later, I took the latest build of ILSpy for a test drive.  Visually, very little has changed.  There are now a couple options that can be set, and an Analysis window is available (though I couldn’t figure out what it was supposed to do or show).  C# and IL are still the only two language options for the decompiler output, even though I had read that VB was in the works.  Still no support for plug-ins, but then I wasn’t expecting it.  One feature that I thought was new was the ability to save the source for an entire assembly all at once, but I double-checked the previous version and it turns out that I’d simply overlooked the feature.  (Still, it’s a nice feature).

What about the killer feature; the decompiler output?  With the new version of ILSpy, I opened up the .NET 4.0 test assembly that I’d created for my previous tests.  Once again, I focused on the more advanced features of the assembly; a method that uses LINQ and a method that uses a dynamic variable.

Just three weeks later, I’m pleased to report that the output of ILSpy is now quite comparable to .NET Reflector.  The decompiler output of the method that uses the dynamic variable is very similar to .NET Reflector’s output (and neither is all that close to the original code).  The output of the method that uses LINQ has been cleaned up as well.  The .NET Reflector output is a bit closer to the original code (no Lambda’s), but both are quite clean.  The body of the method in the decompiler output of both tools is two lines, which is the same as the original code.

In addition, I glanced over ILSpy’s decompiler output of the more basic elements of the test assembly, and found a few minor items that had been cleaned up.

A very impressive improvement in a short amount of time. 

Last month I wrote that if you used .NET Reflector frequently and couldn’t live without, then you should pay for the upgrade.  The alternatives weren’t adequate.  I also advised occasional users to evaluate the alternatives and to keep an eye on their further development.  I now think that even frequent users of .NET Reflector should give ILSpy a test drive.  While still not complete, you might find that it meets your needs.

Alternatives to .NET Reflector

After Red Gate’s recent announcement (http://www.red-gate.com/products/dotnet-development/reflector/announcement) that the .NET Reflector tool will become a paid app, I began to wonder (like many developers, I’m sure) about free and/or open source alternatives.

(No, I’m not wading into the political arguments about the change other than to say that the fact that the last free version of .Reflector is time-bombed… meaning that you’ll have to pay up or quit using the tool altogether… is disappointing.)

.NET Reflector

As you probably know, .NET Reflector is a tool for browsing and decompiling the contents of compiled .NET assemblies.  Originally created by Lutz Roeder and subsequently purchased by Red Gate software, it has always (until now) been a free utility.  Here is a screenshot of the tool in action:

Reflector

The Alternatives

A quick search turned up three potential replacements for .NET Reflector: ILSpy, Dotnet IL Editor, and MonoDevelop.  The following table gives an overview of what each of these tools has to offer.

TOOL .NET Reflector Dotnet IL Editor MonoDevelop ILSpy
PRICE $35 Free Free Free
OPEN SOURCE No Yes Yes Yes
SUPPORTS PLUG-INS Yes No Yes * Yes
DISASSEMBLER Yes Yes Yes Yes
DECOMPILER Yes No Yes Yes
.NET VERSION REQUIRED 4.0 2.0 3.5 4.0
VISUAL STUDIO INTEGRATION Yes No No No
CLICK NAVIGATION OF SOURCE Yes No Yes Yes

* MonoDevelop supports plug-ins.  However, the Assembly Browser itself is a plug-in, so it’s not immediately clear if a plug-in could be added to extend the functionality of the Assembly Browser.

Dotnet IL Editor

http://sourceforge.net/projects/dile/
This tool allows disassembling and debugging of .NET applications directly from the application assemblies.
Version 0.2.6 (latest stable build) is more than three years old, released on November 4, 2007.  Current weekly builds of version 0.2.7 (development appears to have restarted in mid-2010) are also available (those require .NET 4.0).  As I was only interested in the disassembling features of the tool, I did not see a difference between the two versions.  Both 32-bit and 64-bit versions are available, which is nice.

There is no formal installation package for this tool; just download the ZIP archive, unpack it, and run the dile.exe executable.

dile

The major downside of this tool that immediately presented itself is that it is ONLY a disassembler, meaning that it produces only IL rather than C# or VB.NET.  Because of this, it is not a tool that I expect too many developers will view and a reasonable replacement for .NET Reflector.

MonoDevelop Assembly Browser

http://monodevelop.com/
You may be familiar with MonoDevelop; it is an open source IDE designed to bring .NET development to any platform (i.e.  Linux and OSX in addition to Windows).  What it not as well known is the Assembly Browser that is included with MonoDevelop.  This tool was highlighted by Miguel de Icaza in his recent blog post: http://tirania.org/blog/archive/2011/Feb-04.html

In addition to .NET 3.5, MonoDevelop requires GTK# 2.12.9 for installation.  GTK# is a separate download and installation process, which is a little bit of a barrier to use.  The other tools I’m highlighting, including Reflector itself, are very easy to set up and use.  MonoDevelop requires a bit more effort.

Once I had MonoDevelop installed, it’s not immediately obvious where to find the Assembly Browser.  Here’s how to do it.  In MonoDevelop, choose to open an assembly like any other file, but specify the Assembly Browser to open it.  (There’s an extra drop-down on the file/open menu that allows you to specify Assembly Browser… the other option there is “Solution Workbench”).  When you choose Assembly Browser, you get a familiar Reflector-like interface to the contents of the assembly.

MonoDevelop

The Assembly Browser can produce both IL and C# output from the decompiler.  The displayed source can be copied and saved, though I was only able to do this one method or property at a time.

ILSpy from SharpDevelop

http://wiki.sharpdevelop.net/ILSpy.ashx
At this writing, the tool is in it’s infancy.  Introduced in February 2011 (immediately following Red Gate’s announcement of the fate of .NET Reflector), is is the marriage of a decompiler that was a developer’s university dissertation that had been lying on his hard drive a couple years (http://community.sharpdevelop.net/blogs/dsrbecky/archive/2011/02/11/ilspy-decompiler.aspx) and a new UI (http://community.sharpdevelop.net/blogs/danielgrunwald/archive/tags/ILSpy/default.aspx). 

The developers themselves note that the tool still needs a lot of work (http://community.sharpdevelop.net/blogs/danielgrunwald/archive/2011/02/04/ilspy-a-new-net-assembly-inspector.aspx).  On the day that I downloaded the binaries, there were 12 builds available for download from that day alone… so it is obviously very early in the development cycle.  However, the SharpDevelop folks have produced solid tools in the past, so I am optimistic about this one.

ILSpy has no formal installation package; just download, unzip, and run.  My first impression upon running the tool was that it looks just like reflector.

ILSpy

ILSpy produces IL and C# output, and reporting VB.NET is also in the works.  There is an option native to the app to save the decompiled source code (so no need to copy and paste).  The source for entire classes can be saved at once.

Test Results

So, how do these tools perform?  In other words, how well to the decompiler features work?  As the decompiler is the killer feature of .NET Reflector, that’s what these tools need to get right in order to be viable replacements.

Below is the source code of an assembly I used to test these tools.  This was compiled three separate times: once as a .NET 2.0 assembly, once as a .NET 3.5 assembly, and once as a .NET 4.0 assembly.  For the 3.5 assembly, a class that uses LINQ was added to the code.  For the 4.0 assembly, a method that uses a dynamic was added.  This mix of functionality and framework targets provided multiple testing scenarios.

The following code is the version that was compiled with a.NET 4.0 target; the “extra” class and method that exist here (but not in the 2.0 and 3.5 versions) are noted in code comments. 

using System;
using System.Collections.Generic;
using System.Linq;      // Add this when compiling to .NET 3.5
using Microsoft.CSharp; // Add this when compiling to .NET 4.0

namespace Geometry
{
   public interface IShape
   {
       double GetArea();
   }

   public abstract class Shape : IShape
   {
       public virtual double GetArea()
       {
           throw new NotImplementedException();
       }
   }

   public class Circle : Shape
   {
       private const double pi = 3.14159265;

       private double _radius = 0;
       public double Radius
       {
           get { return _radius; }
           set { _radius = value; }
       }

       public override double GetArea()
       {
           return 2 * pi * _radius;
       }
   }

   public class Square : Shape
   {
       private double _height = 0;
       public double Height
       {
           get { return _height; }
           set { _height = value; }
       }

       private double _width = 0;
       public double Width
       {
           get { return _width; }
           set { _width = value; }
       }

       public override double GetArea()
       {
           return _height * _width;
       }
   }

   public class Triangle : Shape
   {
       private double _base = 0;
       public double Base
       {
           get { return _base; }
           set { _base = value; }
       }

       private double _height = 0;
       public double Height
       {
           get { return _height; }
           set { _height = value; }
       }

       public override double GetArea()
       {
           return 0.5 * _base * _height;
       }
   }

   // Add this class when compiling to .NET 3.5
   public class ShapeGroup
   {
       private List<IShape> _shapes = new List<IShape>();
       public List<IShape> Shapes
       {
           get { return _shapes; }
       }

       public void Add(IShape shape)
       {
           _shapes.Add(shape);
       }

       public void Clear()
       {
           _shapes.Clear();
       }

       public double GetTotalArea()
       {
           var totalArea = from n in _shapes select n.GetArea();
           return totalArea.Sum();
       }

       // Add this method when compiling to .NET 4.0
       public double GetShapeArea(int index)
       {
           dynamic shape = _shapes[index];
           return shape.GetArea();
       }
   }
}

 

.NET Reflector

In order to compare the alternatives to the original, included here is the decompiler output of .NET Reflector itself for the Square and ShapeGroup classes.  You can see that the GetShapeArea method looks a bit different from the original source code, but otherwise the output is very close to the original.

public class Square : Shape
{
   // Fields
   private double _height;
   private double _width;

   // Methods
   public override double GetArea()
   {
       return (this._height * this._width);
   }

   // Properties
   public double Height
   {
       get
       {
           return this._height;
       }
       set
       {
           this._height = value;
       }
   }

   public double Width
   {
       get
       {
           return this._width;
       }
       set
       {
           this._width = value;
       }
   }
}

public class ShapeGroup
{
   // Fields
   private List<IShape> _shapes = new List<IShape>();

   // Methods
   public void Add(IShape shape)
   {
       this._shapes.Add(shape);
   }

   public void Clear()
   {
       this._shapes.Clear();
   }

   public double GetShapeArea(int index)
   {
       object shape = this._shapes[index];
       if (<GetShapeArea>o__SiteContainer2.<>p__Site3 == null)
       {
           <GetShapeArea>o__SiteContainer2.<>p__Site3 = CallSite<Func<CallSite, object, double>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(double), typeof(ShapeGroup)));
       }
       if (<GetShapeArea>o__SiteContainer2.<>p__Site4 == null)
       {
           <GetShapeArea>o__SiteContainer2.<>p__Site4 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(CSharpBinderFlags.None, "GetArea", null, typeof(ShapeGroup), new CSharpArgumentInfo[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
       }
       return <GetShapeArea>o__SiteContainer2.<>p__Site3.Target(<GetShapeArea>o__SiteContainer2.<>p__Site3, <GetShapeArea>o__SiteContainer2.<>p__Site4.Target(<GetShapeArea>o__SiteContainer2.<>p__Site4, shape));
   }

   public double GetTotalArea()
   {
       return this._shapes.Select<IShape, double>(delegate (IShape n) {
           return n.GetArea();
       }).Sum();
   }

   // Properties
   public List<IShape> Shapes
   {
       get
       {
           return this._shapes;
       }
   }
}

 

Dotnet IL Editor

I couldn’t easily capture the output for an entire class or assembly at once using the DotNet IL Editor, so here is the output for the GetTotalArea method of the ShapeGroup class. This is one of the more interesting methods in the class, as it uses LINQ.  As mentioned earlier, this tool is a disassembler only, so IL is the only output possible.

.method public hidebysig instance float64 GetTotalArea() cil managed
{
   // Code size 48 (0×30)
   .maxstack 4
   .locals init (class [mscorlib]System.Collections.Generic.IEnumerable`1<float64> V_0)
   ldarg.0
   ldfld class [mscorlib]System.Collections.Generic.List`1<class Geometry.IShape> Geometry.ShapeGroup::_shapes
   ldsfld class [mscorlib]System.Func`2<class Geometry.IShape, float64> Geometry.ShapeGroup::CS$<>9__CachedAnonymousMethodDelegate1
   brtrue.s IL_001e
   ldnull
   ldftn float64 Geometry.ShapeGroup::<GetTotalArea>b__0(class Geometry.IShape)
   newobj instance void class [mscorlib]System.Func`2<class Geometry.IShape, float64>::.ctor(object, native int)
   stsfld class [mscorlib]System.Func`2<class Geometry.IShape, float64> Geometry.ShapeGroup::CS$<>9__CachedAnonymousMethodDelegate1
   ldsfld class [mscorlib]System.Func`2<class Geometry.IShape, float64> Geometry.ShapeGroup::CS$<>9__CachedAnonymousMethodDelegate1
   call
   stloc.0
   ldloc.0
   call float64 [System.Core]System.Linq.Enumerable::Sum(class [mscorlib]System.Collections.Generic.IEnumerable`1<float64>)
   ret
} //end of method Geometry.ShapeGroup::GetTotalArea

 

MonoDevelop

Interestingly, MonoDevelop was only able to decompile the .NET 2.0 test assembly without error.  It had trouble with the GetTotalArea method of the ShapeGroup class in both the 3.5. and 4.0 versions of the assembly.  That is the method which makes use of LINQ.  I’m not sure if LINQ support is a known shortcoming of the current version of the Mono tools, but it highlights a significant drawback to using this assembly browser.

Here is the decompiled source for the Square and ShapeGroup classes, as produced by the Assembly Browser in MonoDevelop.

public class Square : Shape{
// Fields 
private double _height; 
private double _width; 

// Constructors public Square (); 

// Methods
public double GetArea() {
  return this._height * this._width;
}

// Properties
public double Height{
  get {
   public double get_Height()
   {
    return this._height;
   }
  }
  set {
   public void set_Height(double value)
   {
    this._height = value;
   }
  }
}

public double Height{
  get {
   public double get_Height()
   {
    return this._height;
   }
  }
  set {
   public void set_Height(double value)
   {
    this._height = value;
   }
  }
}
}

public class ShapeGroup{
// Fields
private List<IShape> _shapes;

[CompilerGenerated ()]
private static Func<IShape, double> CS$<>9__CachedAnonymousMethodDelegate1;

// Constructors
public ShapeGroup ();

// Methods
public void Add(IShape shape)
{
  this._shapes.Add(shape);
}

public void Clear()
{
  this._shapes.Clear();
}

public double GetTotalArea ();
{
Decompilation failed:
System.NotImplementedException: IL_000e: ldftn float64  Geometry.ShapeGroup::b__0(Geometry.IShape)
    at Cecil.Decompiler.Cil.BaseInstructionVisitor.OnLdftn(Instruction instruction)
    at Cecil.Decompiler.Cil.InstructionDispatcher.Dispatch(Instruction instruction, IInstructionVisitor visitor)
    at Cecil.Decompiler.Cil.BaseInstructionVisitor.Visit(Instruction instruction)
    at Cecil.Decompiler.StatementDecompiler.ProcessExpressionBlock(InstructionBlock block, Boolean skip_first)
    at Cecil.Decompiler.StatementDecompiler.ProcessExpressionBlock(InstructionBlock block)
    at Cecil.Decompiler.StatementDecompiler.PushConditionExpression(Instruction instruction)
    at Cecil.Decompiler.StatementDecompiler.TryProcessExpression(Instruction instruction)
    at Cecil.Decompiler.StatementDecompiler.ProcessInstruction(Instruction instruction)
    at Cecil.Decompiler.StatementDecompiler.ProcessInstructions(InstructionBlock block)
    at Cecil.Decompiler.StatementDecompiler.ProcessBlock(InstructionBlock block)
    at Cecil.Decompiler.StatementDecompiler.ProcessBlocks()
    at Cecil.Decompiler.StatementDecompiler.Run()
    at Cecil.Decompiler.StatementDecompiler.Process(DecompilationContext context, BlockStatement body)
    at Cecil.Decompiler.DecompilationPipeline.Run(MethodBody body)
    at Cecil.Decompiler.Extensions.RunPipeline(DecompilationPipeline pipeline, MethodBody body)
    at Cecil.Decompiler.Extensions.Decompile(MethodBody body, ILanguage language)
    at Cecil.Decompiler.Languages.CSharpWriter.Write(MethodDefinition method)
    at MonoDevelop.AssemblyBrowser.DomMethodNodeBuilder.Decompile(DomCecilMethod method, Boolean markup)
}

public double GetShapeArea(int index)
{
  CSharpArgumentInfo[] V_1;
  object V_0 = this._shapes.get_Item(index);
  if (!<GetShapeArea>o__SiteContainer2.<>p__Site3)
  {
   <GetShapeArea>o__SiteContainer2.<>p__Site3 = CallSite<Func<CallSite, object, double>>.Create(Binder.Convert(0, Type.GetTypeFromHandle(double), Type.GetTypeFromHandle(ShapeGroup)));
  }
}

[CompilerGenerated ()]
private abstract sealed class <a ref="T:Geometry.ShapeGroup.o__SiteContainer2"><GetShapeArea>o__SiteContainer2
{
  // Fields
  public static CallSite<Func<CallSite, object, double>> <>p__Site3;
  public static CallSite<Func<CallSite, object, object>> <>p__Site4;
}

private static double <GetTotalArea>b__0(IShape n)
{
  return n.GetArea();
}

// Properties
public List<IShape> Shapes{
  get {
   public List<IShape> get_Shapes()
   {
    return this._shapes;
   }
  }
}
}

 

ILSpy

The C# produced by ILSpy is good quality (a near reproduction of the original) for simple code.  However, when it decompiled the GetTotalArea() and GetShapeArea() methods on the ShapeGroup class, (which use LINQ and dynamics, respectively), the output was considerably different from the original (and from Reflector’s output). 

In general, ILSpy’s decompiler output does not match the quality of Reflector’s.  However, it appears to produce the cleanest code of the three .NET Reflector alternatives.  Considering it has been in development for less than one month, I think it shows great promise.

Here is the ILSpy decompiler output for the Square and ShapeGroup classes.

namespace Geometry
{
public class Square : Shape
{
  private double _height;
  private double _width;
  public double Height
  {
   get
   {
    return this._height;
   }
   set
   {
    this._height = value;
    return;
   }
  }
  public double Width
  {
   get
   {
    return this._width;
   }
   set
   {
    this._width = value;
    return;
   }
  }
  public override double GetArea()
  {
   return this._height * this._width;
  }
}
}

namespace Geometry
{
public class ShapeGroup
{
  private static class <GetShapeArea>o__SiteContainer2
  {
   public static CallSite<Func<CallSite, object, double>> <>p__Site3;
   public static CallSite<Func<CallSite, object, object>> <>p__Site4;
  }
  private List<IShape> _shapes;
  public List<IShape> Shapes
  {
   get
   {
    return this._shapes;
   }
  }
  public ShapeGroup()
  {
   this._shapes = new List<IShape>();
   base..ctor();
  }
  public void Add(IShape shape)
  {
   this._shapes.Add(shape);
  }
  public void Clear()
  {
   this._shapes.Clear();
  }
  public double GetTotalArea()
  {
   IEnumerable<IShape> arg_23_0 = this._shapes;
   if (ShapeGroup.CS$<>9__CachedAnonymousMethodDelegate1 == null)
   {
    ShapeGroup.CS$<>9__CachedAnonymousMethodDelegate1 = ((IShape n) => n.GetArea());
   }
   return Enumerable.Sum(Enumerable.Select<IShape, double>(arg_23_0, ShapeGroup.CS$<>9__CachedAnonymousMethodDelegate1));
  }
  public double GetShapeArea(int index)
  {
   object item = this._shapes.get_Item(index);
   if (ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site3 == null)
   {
    ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site3 = CallSite<Func<CallSite, object, double>>.Create(Binder.Convert(CSharpBinderFlags.None, typeof(double), typeof(ShapeGroup)));
   }
   Func<CallSite, object, double> arg_95_0 = ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site3.Target;
   CallSite arg_95_1 = ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site3;
   if (ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site4 == null)
   {
    CSharpBinderFlags arg_71_0 = CSharpBinderFlags.None;
    string arg_71_1 = "GetArea";
    IEnumerable<Type> arg_71_2 = null;
    Type arg_71_3 = typeof(ShapeGroup);
    CSharpArgumentInfo[] array;
    (array = new CSharpArgumentInfo[1])[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
    ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site4 = CallSite<Func<CallSite, object, object>>.Create(Binder.InvokeMember(arg_71_0, arg_71_1, arg_71_2, arg_71_3, array));
   }
   return arg_95_0.Invoke(arg_95_1, ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site4.Target.Invoke(ShapeGroup.<GetShapeArea>o__SiteContainer2.<>p__Site4, item));
  }
}
}

 

Conclusions

Having evaluated these tools, my view is that if you are a frequent user of .NET Reflector and can’t live without it, go ahead and pay for the tool.  You’ve probably gotten your money’s worth already anyway.

If you use Reflector only occasionally, and particularly if you don’t have an immediate need for it, give the tools I’ve highlighted a try first… particularly ILSpy.  But be prepared to pay for Reflector in case these tools do not meet your need. 

The final word: keep an eye on the ongoing development of these tools.  While Reflector has the edge today, over time it’s certainly possible that one or more of them will become a reliable replacement for .NET Reflector.

Follow

Get every new post delivered to your Inbox.