C# commandline decompiler - c#

I'm looking to decompile a lot of assemblies into C#. I found several tools that do a visual decompile. However I was wondering if there is a tool that can do decompiling from the command line (similar to ildasm /OUT).
The motivation is that there are 100+ of assemblies and I don't want to open each one and save as an .cs file. .NET Reflector seems to have a batch load of assemblies, but it doesn't have a batch save. So I was thinking of writing a script that goes through each assembly and decompiles it using a command line command.

If you are looking to have a program that generates the C# code for an assembly, Jon Gallant recently had a blog post about doing this using JustDecompile from Telerik. There are a couple of assemblies that you link to and then you can control the generation of the code without a UI.

The only thing you need is this open source decompiler called dnSpy -> https://github.com/0xd4d/dnSpy
includes a command line tool:
Examples:
dnSpy.Console.exe -o C:\out\path C:\some\path
Decompiles all .NET files in the above directory and saves files to C:\out\path
dnSpy.Console.exe -o C:\out\path -r C:\some\path
Decompiles all .NET files in the above directory and all sub directories
dnSpy.Console.exe -o C:\out\path C:\some\path\*.dll
Decompiles all *.dll .NET files in the above directory and saves files to C:\out\path
dnSpy.Console.exe --md 0x06000123 file.dll
Decompiles the member with token 0x06000123
dnSpy.Console.exe -t system.int32 --gac-file "mscorlib, Version=4.0.0.0"
Decompiles System.Int32 from mscorlib

Just as a continuation of the answer from above, dnSpy seems to be discontinued and crashed for me when I tried to use it (like here).
Maybe consider using dnSpyEx, which worked like a charm.

Related

How does C# compiler search for libraries during compilation?

A C# program uses .Net framework libraries or third party libraries by using directive in the C# code.
When compiling the program using csc,
how shall we specify the search path for the used libraries?
does the compiler search for the used libraries in some search order?
I am comparing csc to gcc and javac in terms of specifying search paths for libraries and searching order. For gcc and javac, there are multiple ways to specify search paths,
command line options to them, such as -L to gcc and -classpath to javac;
environment variables, such as LIBRARY_PATH for gcc and CLASSPATH for javac;
default paths, such as current directory, some system-wide directory
Does csc also have multiple ways, and have a search order between them?
Same questions if compiling the program using msbuild which calls csc.
Thanks.
how shall we specify the search path for the used libraries?
Use the /lib command line option.
Syntax
/lib:dir1[,dir2]
dir1
A directory for the compiler to look in if a referenced assembly is not found in the current working directory (the directory from which you are invoking the compiler) or in the common language runtime's system directory.
dir2
One or more additional directories to search in for assembly references. Separate additional directory names with a comma, and without white space between them.
OP
Same questions if compiling the program using msbuild which calls csc
If you use msbuild you are more than likely using a .proj file so it would use the path setup for your environment I would expect
Tell me more
-lib

compile C# solution into DLL for use in python

I am trying to use a specific natural language processor I found written in C# within my python application.
According to past SO posts, it seems as straightforward as:
import compile DLL file
call methods/classes of C# file from within python script with an enclosing wrapper
I have downloaded mono-develop and xamarin studio, have read the docs and attempted to "build" the C# program off of the .sln file, but have not observed any dll file being created
What step or doc am I missing?
https://msdn.microsoft.com/en-us/library/3707x96z.aspx
This link provided from MSDN shows how to compile DLLs. You use the developer command prompt which is installed when you install any version of Visual Studio.
From the link, here is an example format:
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
You can build a solution with xbuild. check this out for more information: Microsoft.Build
or you can just download mono develop and open the solution with it and just click build. it would create the output dll file in the bin folder.

How MSBUILD calls CSC.exe?

I want to know about how msbuild.exe execute a C# application.
As i searched in google i came to know csc.exe,PE File, JIT, IL File. And msbuild.exe internally calls csc.exe to compile a C# Application. So I opened csc.exe in reflector. But There is no call csc.exe from msbuild. So how does msbuild call csc.exe?
Although, EXEs are indeed libraries (like DLLs, EXEs are PE files), they are almost always called by creating a separate process with command-line arguments.
Command-line arguments are passed to the "main" function of the EXE, usually as an array of strings. You could find csc's main function with Reflector.
But, you probably want to know what msbuild passes to csc for a particular build. In that case, just use msbuild's verbosity switch:
msbuild MyProject.csproj /target:rebuild /verbosity:diag
See Obtaining Build Logs with MSBuild.
The call to csc.exe is contained in the according .targets1 script. MSBuild.exe itself is oblivious to which language compiler (c#, vb, ...) you are targeting. This is determined by the solution/project and their references.
The actual call to csc.exe should be located in Microsoft.MSBuild.Tasks.dll in your .NET Framework folder.
1 In case of C# it's Microsoft.CSharp.targets

Running Fortify on .dll files as well as .cs

.NET version of existing question Can I run fortify on .jar files instead of .java?
My team is using Fortify 5.15.0.0060. We are scanning our .NET project in Visual Studio, and project settings shows the following command line arguments:
-vsversion 10.0 "-b" "project" "-machine-output" "-scan" "-f" "C:\Users\user\AppData\Local\Fortify\VS2010-3.80\project\Scan.fpr" "-format" "fpr"
This highlights issues in our .cs source code files, but does not appear to look at the DLLs that form part of our solution (and which are copied to our output directory along with our binaries). For many of these libraries we do not have access to .pdb files, but were hoping that Fortify would be able to do a limited amount of scanning anyway.
Is it possible to add a command line argument to include DLL files, with/without PDB files?
When it comes to .Net, Fortify can only scan assemblies that have a .pdb because it uses ildasm.exe to decompile the assembly and then it uses the .pdb to match to the source files. Unfortunately there is no way for you to scan these extra assemblies.
If you have the .pdb files, you can have Fortify scan them, however you may not see the full source in your results. To include these assemblies, you need to specify them in your Translation options. If you are doing this all from the command line, then this is how you would do it:
sourceanalyzer -b project -vsversion 10.0 projectPath\Additional.Assembly.1.dll projectPath\Additional.Assembly.2.dll projectPath\Additional.Assembly.3.dll
sourceanalyzer -b project -scan -f MyResults.fpr
If you have not yet updated your Fortify version, I would highly recommend that you do so.

How to include libraries (such as fastJSON) in a c# (gmcs) project

Disclaimer: I just made by hello world with gmcs yesterday
Problem
I want to use fastJSON in my project using gmcs.
How do I
compile the project, consisting of 10 or so files into a library?
statically link against that library with my 1-file project?
"install" the library on my system?
Imagined Solution
cd ~/fastJSON
gmcs --blahblah=fastJSON.csproj
cd ~/myProject
gmcs --yadayada=static ~/fastJSON/fastJSON.lib main.cs
The easiest solution is to use xbuild to build the fastJSON project:
cd path/to/fastJSON
xbuild fastJSON.csproj
This will build a library (.dll) somewhere (typically in the bin/Debug subdirectory, but it can be changed in the project file so it may be somewhere else for fastJSON).
Now you have the library, and you reference it like this when compiling your code:
gmcs -r:path/to/fastJSON.dll mycode.cs
You don't need to install the fastJSON library on your system, just put it next to your executable.
according to the man page, you need to use the -r command line option:
gmcs -r ~/fastJSON/fastJSON.dll main.cs

Categories