This week at the PowerShell Summit in Redmond, I gave a talk about using .NET reflection in PowerShell. We took a quick look at ILSpy and how to read the decompiled C# code that makes up compiled .NET assemblies. After a comment by Karl Prosser, I figured it would be cool to take it a step further and actually marry the two. The result was a Get-MemberBody advanced function that outputs the actual C# code for members piped from Get-Member.

I used ILSpy to take apart ILSpy and find the classes it uses to actually disassemble .NET assemblies.  The core piece here is the CSharpLanguage class and the AssemblyDefinition’s ReadAssembly method.


Once all the pieces are in place you can easily call the CSharpLanguage’s many Decompile methods to turn a particular member into a string representation of the decompiled C# code.
decompileFinally, I wrapped everything into an advanced function that accepts pipeline input from the Get-Member cmdlet. This allows for simple pipes like the following.



The output from a pipeline like this is the decompiled source code for AddTicks. It’s a simple string and even includes the XML help!



I’ve posted the advanced function to You’ll need an install of ILSpy. I tested on the latest version (