JustDecompile / dotPeek show me an assembly's source code. How can I modify it?
.net, decompiling
Solution
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.
Other related libraries / frameworks:
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.
Problem
I can see the original source code with the decompilers, but how do I modify the source code and re-compile the .DLL afterwards?