JustDecompile / dotPeek show me an assembly's source code. How can I modify it?


JustDecompile / dotPeek show me an assembly's source code. How can I modify it?
I can see the original source code with the decompilers, but how do I modify the source code and re-compile the .DLL afterwards?
2 Answers
2
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):
ildasm
ilasm
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.
@user101579, I updated my answer.
– stakx
Jun 23 '12 at 22:01
I heard about this ILSpy 2.0 I can decompile the project to a Visual C# project, is that a good product? It does have type or namespace not found errors.
– user101579
Jun 25 '12 at 17:51
I don't know ILSpy, but why don't you give it a try yourself and see if it meets your requirements?
– stakx
Jun 25 '12 at 18:03
A plugin for JustDecompile allows you to edit assemblies. Go to the Assembly Editor Plug-In section.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So, when using those tools, I suppose to generate the source code first, and compile it with another tool? Or can I just insert new code with those tools?
– user101579
Jun 23 '12 at 21:23