.NET Power Tip 1–Save a .NET Assembly as a Visual Studio Project (*.csproj) with ILSpy

 

This blog post introduces ILSpy and shows the lesser known feature that allows you to decompile foreign assemblies and directly save them in the Visual Studio Project format (*.csproj).

You might be familiar with ILSpy (http://ilspy.net/), the open source successor of the legendary .NET Reflector. It emerged in the time when RedGate acquired .NET Reflector and started requesting money for it.

ILSpy is huge. It is one of the tools required in every .NET developers toolbox.

I use it a lot to look inside framework or 3rd party .NET assemblies. ILSpy lets you look at the metadata of the assembly (namespaces, classes, methods, members, etc) and it disassembles the IL code. The following picture shows the FileStream class from the mscorlib assembly:

image

Using the dropdown menu you can let ILSpy decompile the code into C# or VB:

image

A feature a lot of developers don’t know about is that you can save an entire assembly into a Visual Studio project file. Unfortunately, the command cannot be executed via the context menu, it is therefore kind of hidden to the inexperienced user.

In general, ILSpy allows you to save the decompiled C# or VB code to disk. To save the code you have to select an item in the navigation and click File –> Save Code… or alternatively hit Ctrl-S:

image

Depending on the type of the item that is currently selected, ILSpy provides a different output. If a namespace, class or member of a class is selected, the output will be a *.cs file. However, if you select an assembly, ILSpy creates a full Visual Studio Project that you can open in the IDE. The mscorlib example from above looks like this:

image

Another very useful feature is the “Analyze” command reachable via context menu. It displays all the relations to other types grouped by “Instantiated By”, “Used By”, “Exposed By” and “Extensions Methods”:

image

Get ILSpy at  http://ilspy.net/ and Have fun decompiling…