Decompile .pdb file C# (Program database) - c#

I want to decompile .pdb files so I can see where references are pointing to. The projects are being built from a build server. I also want to explore the possibility of using these to index a codebase.
I cannot seem to find any tools to decompile and view the contents.
I have found this stack post Help me to read .pdb file but the first recommended example is missing (Producing an XML would be ideal) && the second will not build on my machine.
I have also tried using reflector to open the file but does not work.
Can anyone provide insight into how I can deompile a .pdb file?

You can try using Mono.Cecil project. It's open source libraries for decompiling .NET assemblies to IL code. It also allow to work with .pdb files of .NET projects. Mono.Cecil doesn't use reflection, it reads assembly as byte stream.
For decompiling assembly to C# code, you can look to the ILSpy project. It's also open source project and it uses the Mono.Cecil library inside.

Related

How to create a dll that includes all the others?

At the moment of creating a project of type "Library of Classes, usually one can generate a dll when compiling, but how could I generate a dll without losing others that I already have included?
I explain with an example: It turns out that Nuget downloaded an S22.Imap dll with the one I worked with, later I generated the dll in the traditional way that I explained in the beginning, but when I wanted to work with dll in another computer, I got errors that were not I found functions that contained the S22.IMAP dll. So to solve this problem, I had to copy the dll of my project, S22.IMAP in an additional way in a specific path of the other computer.
My question is:
How could you generate a dll that includes the ones included in the project you were working with?
All the referred 3rd party dlls (S22.Imap.dll in your example) will be copied to the output folder together with your own dll file (let's say a.dll) when you build your project. That means you should always copy them together (S22 + a.dll) to the place you want to refer them, on another computer/folder/place.
If you really want to make them only one file (although it is not recommended), you can set the S22 one as some "nested resource". Then you will get only one a.dll file and the S22 one is inside the a.dll. See below page for some reference:
Embedding one dll inside another as an embedded resource and then calling it from my code
AND, ILMerge is some tool that can help you do so.
In general, you don't. A DLL is a dynamic linked library, and you would normally only combine static libraries during a build. Here is an answer on the difference between static and dynamic linking.
Typically you would include all the DLLs you need in the installer package. If you use Visual Studio to create the installer, it can detect the dependencies for you. When you run the installer, all of the necessary DLLs are deployed. Nearly all commercial .NET software follows this pattern.
It is possible to merge an assembly into another assembly using a tool called ILMerge. This would be a very unusual thing to do, and could cause issues with intellectual property and code signing, so it is not recommended.

C# class declaration "from metadata"

I've noticed in Visual Studio some classes have an option that if you look at their definition, you don't actually see the definition only the class's declaration and some documentation, for example:
Does anyone know how to make my classes lead to a similar file?
Thanks
I think some basic has to be explained here.
Source code is bunch of C# files (.cs). This is where code of your classes is
When you compile source code you will get an assembly (.dll). Assembly contains metadata about your classes and compiled binary code, but not actual source code.
When you compile your source code, Visual Studio produces also .PDB file along with your assembly. PDB files allows you to see the source code of the assembly. PDB files are necessary for debugging. It is somewhat similar to javascript source maps.
Now, when in Visual Studio you Go to Definition of a class or a method , then following can happen:
The class is in your solution -> you are navigated to the source code file (.cs)
The class is defined in a referenced assembly and PDB file is available -> you are navigated to source code extracted from the PDB file. You can debug it, (however, you cannot edit it).
The class is defined in a references assembly and PDB file is NOT available -> you are navigated the assembly metadata. (this is what happend on the picture you've posted)
So answer to your question is: isolate your assembly from .PDB and source code. However, you should be aware, that there are tools, that can reverse engineer C# code from the binary code that is in the assembly. It will be not exactly the same as your original source code but very similar.
This is the default behavior if Visual Studio can't find the PDB file which contains information on the location of the actual file and the line numbers in the compiled code.
This should work if you include a referenced assembly from another solution, and then rename the folder where the code is contained. Remove the PDB from the referenced location, and you will see this outline.
For you as a developer, showing the code is a feature: it helps you to debug and analyse problems in assemblies you have built. So I would advice to keep it on. Don't worry too much about other developers, if you don't send the PDB file, they won't see the code from Visual Studio. They still can read the assembly's source code using Reflector for example.

dotnet dll decompile and change the code

I need to change the code of the .NET DLL. I am able to see the code by compilable the DLL with .NET reflector, but I am not able to change the code of the DLL. With .NET Reflector, I decompile the code and saved in to my hard disk, but when i am able to recompile the code its giving errors.
Few code is decompiled in binary format few code is decompiled with c#. Is there any tool to change and recompile the DLL?
Here are the tools I used for trying to decompile the DLL:
ILSpy
DisSharp
Reflector7.1 With the Reflexil plugin
Spices.Net.Suite.5.8
Deploy .NET 1.0.0
devextras.codereflect
dotPeek-1.0.0.2545
intellilock
JustDecompile_BETA_2011.1.728.1
Unfortunately, none of the tools giving perfect source code to recompile the DLL code.
The following code is working:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\ildasm.exe" original.dll /out=code.asm
echo Here changes to code.asm should be done.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\ilasm.exe /dll code.asm
So if the change in code is to be small, it's doable by making the changes in assembly code directly. One can compile and disassemble simple methods to see how the assembly code should look like. Putting additional methods inside the assembly file should not be too hard too.
Of course code analyzis should be done using ilspy-like tools, displaying the source code rather than assembly code.
Tools employed here come from Windows SDK (ildasm) and from .net framework (ilasm).
I've had limited success in recompiling DLLs. A better way of going about it is to using Reflector and the Reflexil plugin. You need to have a bit better knowledge of the IL code that makes up .NET assemblies but Reflexil does a great job of describing the OP codes. I have a little walk through on my blog about how I used to modify the PowerShell Cmdlet Help Editor: http://csharpening.net/?p=348

why would a .cs file not see updates to an included dll file

I have a dll containing classes to access data in SQL (a sort of ORM system) included in my .cs page with a using statement. For some reason the dll (with definition for a new field) isn't seen by the cs code, though I've uploaded the new dll in bin. It won't see my new field in the dll's helper classes (now compiled into the dll).
Is there a way to troubleshoot the dll, or the cs to tell why this won't see the class I updated and rebuilt? The class works fine locally and on another server, but on my prod server, it bombs.
This is using Sitefinity 3.7 with a Subsonic/Substage module if that sheds some light on it.
If you are using Visual Studio, verify 2 things, first:
try deleting csproj.user and .suo files (visual studio will recreate them)
The second thing is the version of the framework your project is running, and the version of the framework the dll was compiled in.
If your project is using .NET 4.0 but the DLL was built using 2.0 or similar you may not be able to use it, you can add it, but it wont be loaded.
This sounds so familiar... have you check to see if there is another dll on the path that gets resolved? Dynamic-Link Library Search Order
Make sure that your dll was not registered on the production service in the GAC.
How to extract an assembly from the GAC?
Perhaps you have a local copy of the DLL in your project and the DLL that gets updated is elsewhere.
I tend to think the dll you build is 32 bit (X86) dll. where as you are trying to consume it from project that targets "Any CPU".
Is your production server a 64 bit ?
If answer is yes, goto project properties => Build tab (of your cs code's project which is not understanding the dll) and set the Platform target as X86.
If the updated DLL has a different version number, you may need to update the Project Reference to it by deleting and re-adding a reference to the DLL in the bin folder.
If the project generating the DLL is present in the same solution, you may have an issue in creating a file reference (may not be updated) instead of a project reference (will be updated).
fuslog.exe is a great tool when troubleshooting assembly (dll) binding issues.
http://msdn.microsoft.com/en-us/library/e74a18c4.aspx
Another .net developer helped me figure this out. I had a rogue ToString() in there where there should have been a cast to string, allowing nulls. My dll was okay after all. Thanks everyone for your suggestions, I learned a lot.

C#'s equivalent of jar files?

Java provides the jar file so that all the class files and jar files are merged into one file.
Does C# provide equivalent/similar functionality?
.NET compiles into dll or exe. You can use ILMerge to merge several dlls/exes into one.
Aren't .NET assemblies just for this?
Remember, you can include resources, etc in it.
Also, assemblies could be combined using ILMerge, and for more complex scenarios you probably should better use ClickOnce or MSI deployment.
For silverlight, there's XAP packages, but I assume you're talking about desktop .NET.
It's called an "assembly".
http://en.wikipedia.org/wiki/.NET_assembly
The jar equivalent in C# (basically in any .Net language) is dll (for class library) and exe (for executable one) or collectively assembly. But one assembly can not include another assembly in the form of dll or exe. But ILMerge do merges two assemblies but not include one in another like jar file.
But there is project published in codeproject (http://www.codeproject.com/KB/install/NARLoader.aspx) you might get interested in. It do stuff like jar files with the .net assemblies.
Not really, C# works with .dll and .lib. If you don't put everything in the same project (all source code), you won't be able to achieve what you probably want to do.
But with ILMerge, you can combine everything into 1 executable for easier distribution if you don't want to have a setup or a compressed file containing all the files needed..
yes C# provides dll
Dynamic-Link Libraries
No, an assembly AFAIK can not include referenced assemblies.

Categories

Resources