Is there a tool which allows me to decompile a .net dll, edit it and repack it back to dll again?
I use dotPeek a lot. It can't repack or edit.
While Reflector is cool - but it's not free.
There is Mono.Cecil - which is a lower level tool that can modify assemblies. The two I now use most often are:
JustDecompile and DotPeek
Reflector is one of a kind. I have successfully created c# projects from binaries and recompiled them.
Bear in mind that protected assemblies are difficult to decompile, also they may be obfuscated or protected against ILDASM.
Related
I am creating a complex class with AssemblyBuilder that Im later creating objects from. There is however uncertainties in how this class is really contructed. So is there any way to write this dynamicly created class to a cs file for inspection?
I can get the dll file written to disk but I need the cs file.
You can decompile managed .NET dll to C# code using
DotPeek by JetBrains (free, sources are closed)
ILSpy open source project (MIT license sources are available at github)
Reflector by Red Gates (Paid tool, sources are closed)
JustDecompile by Telerik (free with open source decompilation engine available at github Apache License)
There is also a Microsoft's ildasm tool.
If you need to write custom tool you can download open-source code and give it a try.
Do you have a requirement to use AssemblyBuilder? I'm asking because AssemblyBuilder wont allow you to see the generated class without using a decompiler and if the class you´re generating is quite complicated, the decompiled code wont be of good quality.
You are almost in the same situation if you use Reflection.Emit because it generates low level IL.
If you really need to see the source code that you're generating dynamically your best option is CodeDom. Here's an example How to: Create a Class Using CodeDOM
You might be able to kill two bird with one stone with Roslyn (aka ".NET Compiler Platform"). You'll need the package Microsoft.CodeAnalysis.CSharp.
First, you can use the SyntaxFactory class to generate syntax nodes, which you can combine into larger structures (members, methods, classes, namespaces, compilation units).
You can also get a nicely formatted representation of your syntax nodes with ToString() or ToFullString() (with correct indentation and line breaks and everything), which is what you were originally looking for.
There are quite a few tutorials online on how to use this API (like 1, 2), and there's the Roslyn Quoter website that can convert a piece of C# code into SyntaxFactory calls.
Second, you can then use the resulting CSharpSyntaxNode to create a CSharpSyntaxTree, which you can compile into IL with the help of CSharpCompilation (after all, Roslyn is the reference C# compiler).
If you want, you can even emit the generates assembly into a stream, get the assembly's binary data from there, and load your newly created assembly into your currently executing assembly, and dynamically instantiate the types you just defined.
You need to use the .NET reflection.
Ildasm.exe cannot help you because it will not create the .cs file you need.
So either the ILSpy is the open-source .NET assembly browser and decompiler from the SharpDevelop team or dotPeek from Jetbrains.
Depending on the platform you may also check Mono Cecil. Cecil is a library written by Jb Evain to generate and inspect programs and libraries in the ECMA CIL format.
If you need speed JustDecompile from Telerik is a free tool for .NET assembly browsing and decompiling that claims to be 10x faster than competitors.
All these tools lets you take an existing compiled assembly (.dll or .exe) and easily browse the symbols it contains, and then just as easily decompile the assembly language back to readable C# and IL.
I have an assembly which is not strong-named. I have no source code for it.
I now need to recover the source code . Is there a way to do this?
Use decompiler such as dotPeek or ILSpy.
Be aware of the fact that code is decompiled using IL code that resides in .NET assembly it may be different from the original code used when compiling the assembly.
I can recommend RedGate .NET Reflector. You can try 14 days trial version.
At work we used to use Reflector, until we learned that dotPeek is free. It serves us well.
I use reflector to decompile a asp.net dll, after that I find the bug and fixed it, now I want to compile it back to a dll, then I can deploy, but it seems that I can't how can I do this ?
You have two choices, either you use Reflector's addin Filedisassembler, which is free, or commercial ( and pricy) .net dissemblers such as spices.net or salamander decompiler.
I used Filedisassembler; the quality of the decompiled code is very bad. The decompiler code cannot be recompiled.
I'm sure about the quality of commercial decompilers but I suspect that it would be much better.
Edit: To use Filedisassembler, go to Reflector, View->Addins, click Add and select the Filedisassembler.dll.
If the fix is simple enough and you know IL, you are better of round tripping it using IL instead of a high-level language such as C# or VB.Net.
See search: http://www.google.com/#hl=en&q=ildasm+IL+round-tripping
I am well aware that one can use reflector to browse the content inside an assembly, and one can use FileDisassembler to convert the content into the c# source code with cs projects. But the source code outputted by FileDisassembler may not be able to compile if it has interface with property.
Is the other similar applications that do what FileDisassembler does?
I would not trust Reflector's decompiler.
Many times I have seen it just ignore instruction it did not understand, or just optimized certain sequences away, and changing the meaning the process.
The only trusty way is to use IL.
Regarding more tools, look at the CCI. IIRC, they had a C# source emitter at some stage, but it was removed for some reason.
dotPeek from jetBrains is a good decompiler for c#. http://confluence.jetbrains.net/display/NETPEEK/dotPeek+Early+Access+Program
Under VS2008, I'm working on a solution containing various DLL project.
Is it possible to obfuscate the whole code?
You could use DotFuscator.
I use ILMerge to merge my assemblies into one, then run that single assembly through the obfuscator of choice. ILMerge has an option to change all types to Internal - I use that flag to increase the obfuscator's effectiveness. ILMerge works just fine on .NET 3.5 executables - I haven't had a chance to test it with the beta bits of .NET 4 yet, however.
All of this can be done transparently by adding a post-build command to your final executable.
The obfuscator I use, by the way, is the free Babel Obfuscator.
You can obsfucate any available source, but not pre-compiled libraries.
If you really must, you can modify your build process to use ILDASM or Reflector to write out the assemblies as source, then obfuscate that as you would normally.