I can see the original source code with the decompilers, but how do I modify the source code and re-compile the .DLL afterwards?
You can export a decompiled assembly as a project in Visual Studio. This feature is now available in dotPeek 1.1 EAP and higher. However, dotPeek can't be used to edit disassembled code.
To open the Modules window, select Debug > Windows > Modules. Once you have identified a module that requires decompilation, you can right-click on the module and select “Decompile Source to Symbol File”.
You can double-click types and type members to display decompiled code (or code from original source files if they are available) in the code viewer. Under assembly node, you can also find the References and Resources nodes.
First of all, you do not see the original source code; you see reverse-engineered source code.
If you are interested in programmatically modifying an assembly, have a look at these projects: * The Common Compiler Infrastructure (CCI) Metadata and CCI AST projects on CodePlex. That's exactly what they are for.
"[CCI] allows applications to efficiently analyze or modify .NET assemblies, modules, and debugging (PDB) files." — from the project's website
IIRC, CCI Metadata works at a fairly low level of abstraction. If you want to modify or insert code with it, you'll have to deal with Intermediate Language (IL). CCI AST provides higher-level abstractions (abstract syntax trees).
(Btw. I suppose CCI won't allow you to tamper with strongly-named (signed) assemblies, since that would defeat their purpose.)
Mono.Cecil. It was developed for Mono, but works on .NET too:
"In simple English, with Cecil, you can load existing managed assemblies, browse all the contained types, modify them on the fly and save back to the disk the modified assembly." — from the project's website
Btw., there's a question here on Stack Overflow that asks for a comparison of the two.
Roslyn. This project's focus is on the representation of source code as abstract syntax/parse trees, and allows you to do e.g. source code analysis and manipulation. It's also supposed to be able to compile source code for you. However, Roslyn is not specialized on assembly manipulation. (To summarise, you could think of Roslyn as a mix of System.CodeDom and System.Linq.Expressions, but much more powerful than either of these.)
Kevin Montrose has written an IL-emitting library called Sigil which is "A fail-fast, validating helper for DynamicMethod and ILGenerator."
You can use the ildasm
(IL disassembler) and ilasm
(IL assembler) to do something called "round-tripping" (read about it e.g. on Mike Stall's blog):
Round-tripping is where you decompile an app (perhaps via ILDasm), potentially edit the IL, and then recompile it (perhaps via ILAsm). This is the backbone for rewriting an assembly [...].
A last option is the System.Reflection and System.Reflection.Emit functionality in the .NET Base Class Library (BCL). The former allows you to inspect assemblies. The latter provides functionality to generate & save new assemblies to disk. As with CCI, you'll have to deal with raw IL. Also, I don't think they allow you to modify an existing assembly easily.
A plugin for JustDecompile allows you to edit assemblies. Go to the Assembly Editor Plug-In section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With