Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 995
user101579 Avatar asked Jun 23 '12 20:06

user101579


People also ask

Can you edit code in dotPeek?

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.

How do I decompile a source 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”.

How do I use dotPeek decompile?

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.


2 Answers

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.

like image 96
stakx - no longer contributing Avatar answered Sep 21 '22 20:09

stakx - no longer contributing


A plugin for JustDecompile allows you to edit assemblies. Go to the Assembly Editor Plug-In section.

like image 26
kodefuguru Avatar answered Sep 23 '22 20:09

kodefuguru