How to recompile IL - c#

So I need to make a very simple change to a DLL file.
I have successfully exported the dll file to IL language with Reflector, and have found the change I need to make (its just changing a URL). So if I make the change in notepad and save it, well thats great & easy, but now I cannot figure out how to build it back into a DLL file. I have tried to export it to a C# project which works, but building it gives me so many errors.
VS doesn't want to open it since exporting to IL doesn't provide a project file.

Just like VisualStudio ships with ildasm, it also ships with ilasm. So you should be able to do:
ilasm /dll myapp.il

Figured it out, works perfectly:
decompile: ildasm file.dll /out:file.il
edit using notepad
recompile: ilasm file.il /DLL /fold /nologo /noautoinherit
.. and of course you can use your favourite decompiler tool such as VS or dotpeek to view the code nicely, then find it in the dirty IL version to edit it.

I used Telerik JustDecompile. from a DLL I can create my project (right click on the assembly / create project c#/vb or IL), I open with VS, I change and I recompile.
Test it !

Related

Is there a way to browser Source Code when pressing F12 if I only have the DLL and PDB files?

I have a very large solution (384 projects), and I usually work in only one of these projects at a time. So when opening that project alone, pressing F12 brings me only to the metadata page of the used DLL. My project has reference to the DLL files created in the bigger solution.
Is there a way to browse the Source Code when pressing F12 if I only have the DLL and PDB files?
There's no official way of doing so, but since a lot of years, there's a tool/extension called Reflector which disassembles MSIL into readable C# code.
It works exactly as you want... You press F12 and it decompiles the code to show C# code. BTW, it won't never be the exact source code as you developed in your solution. It's a translation back from MSIL to C#.
It's not free, but AFAIK, there's a trial and maybe it's worth the price for you...
JetBrains (makers of ReSharper) offer their own MSIL decompiler called dotPeek https://www.jetbrains.com/decompiler/
You can enable source server support and it will show you the decompiled source code when you F12.

.NET Reflector - How do I Import a file that I exported as Assembly Source Code previously

I actually exported an .exe with the "export assembly source code" option to work better with it, but when I did all the changes I wanted to do, I realized that I didn't knew how to import everything up again in an exe.
Can someone help me with my problem?
If you want to inject C# code into compiled assemblies, you can use Reflexil.
Note, that it runs as a plugin on top of Reflector, ILSpy, or JustDecompile.

How to decompile ASP.NET web application [duplicate]

we have an application in production and the code is in Pre-Compiled form. The developer who developed that application left the company and we don't have any backup of source code. the only access we have is Pre Compiled code in the server. We need to fix the issues in the application now.
Is there any way to Decompile (extract to actual source code) the PreCompiled code ?
Open the DLLs in the .NET Reflector.
To update #xOn's answer:
Due to a recent source control disaster I had to recover my project from the DLLs. So sadly I'm knowledgable on the procedure:
Get .Net Reflector from here: http://www.red-gate.com/products/dotnet-development/reflector/
You do not seem to need FileDisassembler.
Either load your bins in Reflector or double click them.
Mind that the .Net framework might have not maintained your original project file structure. So if some of your ASPX pages shared the same basic class name in different .cs files (I.E. Inherits tag is the same, but CodeFile tag differs), you would not be able to simply "export source code".
Don't go blindly exporting source files. It won't work.
There will still be some work to be done before being able to just fire up the old build button. For example - DLLs can refer to property's getter/setters directly. You will have to fix that.
Good luck.
Start with:
http://www.red-gate.com/products/dotnet-development/reflector/
...plus:
http://www.denisbauer.com/NETTools/FileDisassembler.aspx
You'll have to recompile the latter to link it against v6.5 of reflector (the latest, as of this post.) It is a good add-on if you want to avoid having to copy and paste ever single class into files by hand. It will dump an entirely assembly as .cs files. I think it may even generate a csproj.
Both tools are free.
I had the similar issue and used Reflector to Decompile it. I got the source code, then changed the bit I wanted, and rebuild it. Then I copied that dll again to Production site. It started to reflect my changes. It was very easy and not at all difficult, maybe because Precompiled site had dlls for every page, and was updatable , so had only code-behind file in dll.
For reference: http://www.reflector.net/
When u install .net reflecter.its import in visual studio.
then you saw the .net reflector tools on menu bar.
Click on .net reflector >>Choose Asseblies (Dll file) to debug.
I found that dll can be just Read using the .NET Reflector tool but can't extract the Source Code. And also once the webforms are precompiled, we can't even get back the code behind files. All we can do is to debug and analyze the Code.

Writing assembly code for the .Net platform

I'm a seasoned C# developer who wants, for fun, to write a bit of assembly code. I was wondering if it was easiest simply to write in byte code and somehow use the C# compiler, linker whatever. I'm a bit unsure on how to go about this.
Or maybe there is a decent assembly language out there with a step debugger and other nice things shipped in an environment that I should pick up instead?
I mainly want to write code to do calculations and output to the console window.
You can write IL code and compile it with ILASM
You can write it in MSIL and assemble it via the MSIL assembler (ilasm.exe).
As for a debugger, I've previously used DILE (DotNet IL Editor) to step through IL code, it's a bit out-dated though.
Seems that DILE is still being updated, check out the weekly builds.
you can use assembly language of .net environment which is called CIL
http://en.wikipedia.org/wiki/Common_Intermediate_Language
You can't use the C# compiler to write assembly code. However, you can you Visual Studio "CLR" projects which will compile native C/C++ with inline assembly blocks, which you can write a managed wrapper around so you can invoke via C#. See CLI/C++ Tutorials for more information.
You can also look at masm32 which you can use to write native x86 assembly libraries, then use p/invoke to execute them via C#.
Have fun!
This may be helpful to others
As for editing MSIL, we can dump assembly file to IL file by the ILDASM utility and recompile it to assembly file by the ILASM utility,
The two utilities are included in the .NET SDK.
C:\Program Files\Microsoft Visual Studio 8\VC>ildasm .
Open your assembly e.g. my.dll
After opening the file, select File->Dump. Ensure all checkboxes are selected and then click OK.
You will be prompted to save it as an IL file. I recommend creating a new directory to save as my.IL
Open my.IL by your favourite editing tool (I use Visual Studio .NET 2005/2003).
Edit your my.IL and then save it.
C:\Program Files\Microsoft Visual Studio 8\VC>ilasm /DLL X:\Folder\my.IL

Using Reflector To Create VisualStudio Project

I have a .exe app which I want to understand better - I can see it in reflector
Is there any way to get reflector to create a VS project with the code so I can view it properly in Visual Studio?
Nothing special is needed, it is built into Reflector, albeit not very discoverable. Right-click the assembly in the left pane and choose Export. You'll get a chance to change the output directory. Click OK and Reflector starts decompiling the code, creating a source file for each individual class. And creates a .csproj file which you can open in Visual Studio.
Check out Jason Bock's FileGenerator, it might be what you are looking for.
I've used Denis Bauer's Reflector.FileDisassembler http://www.denisbauer.com/NETTools/FileDisassembler.aspx. It works well enough to compile and step through the code.
Yea there is, but it doesn't come cheap
http://www.remotesoft.com/salamander/
I have used it to decompile assembly, but I've never used the feature to decompile it into a project so can't give you a review on that. The quality of the decompiler will match the one in reflector.
They also be some legal issues associated with decompiling exe into project - and source for recompilation, so use it with care.

Categories